Reputation: 6871
I just started learning about writing unit tests with pytest
but I cannot find anything about testing to check if 2 classes work properly together where one class takes the output of the second class, such as
assert bar.bar(foo.foo('hello')) == 'hellofoobar'
Should pytest be used for running such tests that involve 2 classes? If so, should this test be written in test_foo.py
or test_bar.py
?
Will such a test be known as integration tests?
Foo.py
class Foo:
def foo(self, x):
return f'{x}foo'
Bar.py
class Bar:
def bar(self, x):
return f'{x}bar'
conftest.py
import pytest
from .Foo import Foo
from .Bar import Bar
@pytest.fixture
def foo():
return Foo()
@pytest.fixture
def bar():
return Bar()
test_foo.py
def test_foo(foo):
assert foo.foo('hello') == 'hellofoo'
test_bar.py
def test_bar(bar):
assert bar.bar('world') == 'worldbar'
__init__.py
Upvotes: 1
Views: 395
Reputation: 4572
I think this is perfectly fine. It expects that both Foo
and Bar
are working properly. In that case, I think you could call it an integration test.
test_bar.py
def test_bar_with_foo(bar, foo):
assert bar.bar(foo.foo('hello')) == 'hellofoobar'
The need for this type of test might depend on how complex each class is. In the stated case (which is super simple) I think it would be ok to get away with breaking up the tests; meaning if test_bar.test_bar
passes and test_foo.test_foo
passes then test_bar.test_bar_with_foo
becomes redundant.
If we break down the test and assume Foo
returns the correct thing the test starts to look something like the below. At which point the question arises; does this test provide further value beyond test_bar.test_bar
?
test_bar.py
def test_bar_2(bar):
assert bar.bar('hellofoo') == 'hellofoobar'
Upvotes: 1