by mumblemumble · 5y ago · 0 comments
It also encourages tests that do too much. If the test is named "test_refresh", well, it says right there in the name that it's a test for any old generic refresh behavior. So why not just keep dumping assertions in there?
I'm much more happy with names like, "test_displays_error_message_when_refresh_times_out". Right there, you know exactly what's being verified, because it's been written in plain English. Which means you can recognize a buggy test implementation when you see it, and you know what behavior you're supposed to be restoring if the test breaks, and you are prepared to recognize an erroneously passing test, and all sorts of useful things like that.
class TestAuthenticateMethod:
class TestWhenUserExists:
class TestWhenPasswordIsValid:
def test_it_returns_200(self, app, user):
assert ...
class TestWhenPasswordIsInvalid:
def test_it_returns_403(self, app, user):
assert ...
class TestWhenUserDoesNotExist:
def test_it_returns_403(self, app, user):
assert ...
It is so worth it. I can look back at my tests 3 months later and know exactly what was intended wthout duplicating explanations with comments. Yes some methods can get a bit long and indented, but it's worth it for me. The other downside is you have to start all classes/methods with "test" but I got used to that very quickly.When you run pytest it's very clear what's being tested and what the expected outcome is since it gives you the full class hierarchy in the output.
E.g. in test_authenticate_method.py:
def test_returns_200_on_valid_password(app, user_exists):
...
def test_returns_403_on_valid_password(app, user_exists):
...
def test_returns_403(app, user_not_exist):
... foo
<snip 1000 lines>
bar
<snip 553 lines>
baz
<snip 243 lines>
quxIn tests, however, I prefer not to have them as my favorite test runners replace the full name of the test method with its docstring, which makes it a lot harder to find the test in the code.
def matriculate_flange(via: Worble):
"Matriculates flange via a Worble."Test function names are less sensitive than regular functions, since they're not explicitly called, but I still don't want to read a_sentence_with_spaces_replaced_by_underscores.
As far as whether or not people do a better job with descriptive test function names, what I've seen is that they do? I of course can't share any data because this is all in private codebases, so I guess this could quickly devolve into a game of dueling anecdotes. But what I've observed is that people tend to take function names - and, by extension, function naming conventions - seriously, and they are more likely to think of comments as expendable clutter. (Probably because they usually are.) Which means that they'll think harder about function names in the first place, and also means that code reviewers are more likely to mention if they think a function name isn't quite up to snuff.
And I just don't like to cut those sorts of human behavior factors out of the picture, even when they're annoying or hard to understand. Because, at the end of the day, it's all about human factors.
pytest does not though.
How are they, in practical reality, better than comments?
prior to mypy/type hints it allowed you to document the types of a function
PS: How come your setup and teardown operations are so expensive in the first place? Why do you even need to set up an entire database? Can't you mock out the database, set up only a few tables or use a lighter database layer?
[0]: https://docs.python.org/3/library/functools.html#functools.c...
We've also found over the years that if you want to be sure everything works on version X of database system Y, you run your CI tests on that.
That way, all your unit tests reads: “test that error message is displayed when refresh times out” etc.
I think it’s a really nice way to lay things out and it avoids all the “magic” of some functions being executed by virtue of their name.
In my experience, when a unit test has no clear name, this is usually a sign that the test does too much (meaning that it's actually not a unit test anymore) or even comprises multiple tests, leading to all the known bad consequences this has (leaking state between tests, unclear test assumptions etc.)
Assert foo(), "foo should bar"
So that each failure has a clear meaning.
Indeed, i may have several assert in a test.