Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Classes for methods under test #3

Open
bhardin opened this issue Dec 21, 2017 · 7 comments
Open

Classes for methods under test #3

bhardin opened this issue Dec 21, 2017 · 7 comments

Comments

@bhardin
Copy link
Contributor

bhardin commented Dec 21, 2017

Discussion on best practice of Classes for methods.

@rbusquet
Copy link

rbusquet commented Dec 21, 2017

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:

  1. resembles the structure of using TestCase
  2. can get a little verbose for simple tests.

Waiting for responses on that matter :) awesome job on getting this together.

@bhardin
Copy link
Contributor Author

bhardin commented Dec 21, 2017

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.

@wagnerluis1982
Copy link

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.

@wagnerluis1982
Copy link

wagnerluis1982 commented Jan 5, 2018

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!

@JamesHutchisonCarta
Copy link

JamesHutchisonCarta commented Jun 12, 2020

Typically I write a test class per class under test. I.E.

class TestCar:
    def test_open_door_changes_air_pressure():
         ...

Having a test class per function under test feels over-verbose to me.

@JamesHutchisonCarta
Copy link

Additionally the test structure should, IMO, try to mirror the actual code structure, but with test_ in front.

@rbusquet
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants