by MrPowers · 5y ago · 0 comments
test_refresh_failure
test_refresh_with_timeout
These get even longer like test_refresh_with_timeout_when_username_is_not_found for example.pytest-describe allows for a much nicer testing syntax. There's a great comparison here: https://github.com/pytest-dev/pytest-describe#why-bother
TL;DR, this is nicer:
def describe_my_function():
def with_default_arguments():
def with_some_other_arguments():
This isn't as nice: def test_my_function_with_default_arguments():
def test_my_function_with_some_other_arguments():Something like this might be a good compromise:
def describe_my_function(register):
@register
def with_this_thing():
...
I think most Python devs understand that "register" can have a side-effect. class Test_my_function:
def with_default_arguments(self):
def with_some_other_arguments(self):
If you can make your eye stop twitching after seeing snake cased class names, this is at least another option of grouping tests for a single function.With rspec, you use the describe and context keywords.
At one level, yes, it's mainly syntactical sugar. As the test-writer, the two approaches may seem interchangeable.
Where I find it really helps is when I'm not the test-writer but rather I'm reviewing another developer's tests, say in PR. I find this syntax and hierarchy produces a much more coherent test suite and makes it easier for me to twig different use cases and test quality generally.
> With pytest, it's possible to organize tests in a similar way with classes. However, I think classes are awkward. I don't think the convention of using camel-case names for classes fit very well when testing functions in different cases. In addition, every test function must take a "self" argument that is never used.
So there's no reason to do this, aside from aesthetics.
I'd recommend against doing un-Pythonic stuff like this, it makes your code harder to pick up for new engineers.