Write unit tests should be as fun as writing your feature code, no matter which language you're using, it's all code at the end of the day.
How it can be improved
Sending the right message is key in my point of view to improve your unit test culture, you don't write unit tests because of the most advanced framework or the best methodology out there.
They exist as a tool to help you to achieve the goals, which is to have a stable, reliable and documented codebase.
How the tools help
The framework will help you to mock calls to external sources that sometimes are 3rd party libraries, that we may don't need wanna include in your test cases.
The methodology let's say TDD(Test-Driven Development) is a really helpful one, but I don't think that it's the only way of doing it, depends on your project, if there's legacy code, the timing of task and so on. You may find yourself writing together or at the end of your feature code.
Let's have a bit of fun
Just to have some code here, and if you're at the beginning of your developer journey let's see how a simple test looks like in python.
A python class that prints the greetings message as ASCII art and return the base string.
from art import *
class HelloClass:
def __init__(self):
self.HELLO_MSG = "Hello world, "
def greetings(self, name):
msg = f"{self.HELLO_MSG}{name}"
text_art = text2art(msg)
print(text_art)
return msg
Then its unit test will be like this one.
import unittest
from python_class import HelloClass
class TestHelloClass(unittest.TestCase):
def test_greetings(self):
hello_class = HelloClass()
self.assertEqual(hello_class.greetings('developer'), 'Hello world, developer')
Conclusion
Make sure that you plan the right time for your task, a unit test can be sometimes 40% of it, adapt to your project's needs and if the culture is broken, include it step-by-step.
It's not easy to unit test a whole codebase that hasn't been planned to have it before. That's my main point from the title, the unit test culture could be broken for a variate of reasons, but there's always a room for improvements.
Hope you enjoy it, that's my personal option about it and based on my experience.
Top comments (14)
You need to consider the problems you're trying to solve with unit tests.
These may be:
Once you understand what problem you're trying to solve, you can try to figure out how well unit testing achieves these for you, or if there a better alternatives.
But if you don't know why you're doing unit testing, except that TDD is good, then you won't be able to make a reasoned assessment, since your position will be ideological.
So, I suggest focusing on the actual problems rather than on a particular approach.
Thanks for enriching the discussion, completely valid points. I wrote this more because of past experiences and talks with friends. Unfortunately, some projects nowadays still don't have a good unit test base. I'm lucky enough to have it working quite well in my projects.
Writing tests early helps a ton. It doesn't have to follow TDD to a T, but often we are writing code, then checking the output of a bunch of things. Unit Testing is an amazing tool to help speed up this process and give us confidence in future releases.
Stop manually checking things and just write a test already!
I guess it's broken because there was a rushed sprint and someone forgot to update test cases
In my experience this is one of the biggest causes for tests not getting written. Rushed feature pushes. So what gets left out? Tests
Or it's not engrained into team culture.
Couldn't agree more ;)
I find writing tests to be quite enjoyable, mostly unit tests because of the fast feedback and the lack of dependencies I have to mess with. I use unit tests mostly for checking things work, being sure on edge cases and documentation. The thing that ruins the fun of writing tests is TDD, so I write tests after the code and that works well for me.
Having too much reliance on unit tests is the pitfall here, they're usually brittle and make refactoring more expensive, which will often be mocks being wrong after the change.
So... why is unit test culture broken?
Thanks Ben, well in my point of view, there're a number of things... to point out a few I would say:
Those are some that I can tell, make sense?
I guess you're mostly saying that it's a problem because people don't do them? I thought from the title you meant that people were doing unit tests but something was wrong with the way they were done.
Wow, got it... yes I meant to say more about the culture of doing it itself.
Its not totally broken, Its followed by many testers. There are some rules and techniques to follow. May be this will be helpful. dev.to/campkathleen3/explain-unit-...
Great man.
Itβs about culture