-
Notifications
You must be signed in to change notification settings - Fork 5
Classes for methods under test #3
Comments
What if a single test covers a simple method. If we have the files named after the class, say a class has a simple method that would be tested with a single test, Say: # -- some/package/main.py --
class ClassA:
def capitalize_text(self, text):
# ...
# -- tests/unit/some/package/main/class_a.py --
# this is a single test that would cover the expected result
def test_capitalize_text():
assert ClassA().capitalize_text('test text') == 'Test text'
# the convention requires this to be like
class TestCapitalizeText:
def test_capitalizes_text(self):
assert ClassA().capitalize_text('test text') == 'Test text' Enforcing the idea of pytest as a functional library for tests, I believe using classes:
Waiting for responses on that matter :) awesome job on getting this together. |
It may be a little more verbose (1 line) in simple test cases. However, as classes begin growing and doing more, the structure is already setup for the next person to add to. I see it as an "Explicit is better than implicit" and "convention over configuration". I see it as keeping tests together by class helps encapsulate behavior. |
I have to say that, in general, I would never use this convention, too many classes to create. However I could have another opinion in face of bigger projects than I'm used to. |
Additionally, I can see that this convention of one class per method is logical, after all we are talking of unit tests, so we should not mix things! Congrats for the good work! |
Typically I write a test class per class under test. I.E.
Having a test class per function under test feels over-verbose to me. |
Additionally the test structure should, IMO, try to mirror the actual code structure, but with |
It's a matter of code organization. How would you visually identify groups of tests that map to a class method? If the file name maps the class we're testing, it makes sense to group tests on different methods with different classes. |
Discussion on best practice of Classes for methods.
The text was updated successfully, but these errors were encountered: