Reputation:
In organising tests in PyTest
, I have seen that the test methods can be defined within a test class, like so:
class TestBasicEquality:
def test_a_equals_b(self):
assert 'a' == 'b'
If we want to write a test (test_client
) that has to use a PyTest fixture client
we do something like this:
def test_client(client):
# assert client.something == something
But how can we organise the test_client
within a test class? I have tried using @pytest.mark.usefixtures(client)
as a decorator for the test class with no success.
Can someone show how and/or point to a guide/documentation for me to understand?
And perhaps a question hidden behind all this: when should we (or shouldn't) put pytest tests within a class? (only now starting to learn PyTest..) ?
Upvotes: 0
Views: 2716
Reputation: 8604
In your given case you would simply include the fixture as another method argument:
class TestSomething:
def test_client(self, client):
assert client.something == "something"
So what are the classes good for? Personally, I rarely have a need to use them with pytest but you can for example use them to:
pytest ./tests/test.py::TestSomething
@pytest.mark.usefixtures()
that you have discovered.class
: @pytest.fixture(scope="class", autouse=True)
Upvotes: 1