In the py.test docs it describes declaring factory methods as fixtures, like-so:
@pytest.fixture
def make_foo():
def __make_foo(name):
foo = Foo()
foo.name = name
return foo
return __make_foo
What are the benefits/tradeoffs of doing this over just defining a make_foo function and using that? I don't understand why it is a fixture.
Actually, the most important advantage is being able to use other fixtures, and make the dependency injection of pytest work for you. The other advantage is allowing you to pass parameters to the factory, which would have to be static in a normal fixture.
Look at this example:
You could now write a test that gets a
connected_client, but you can't change the port. What if you need a test with multiple clients? You can't either.If you now write:
You get to write tests receiving a
connect_clientfactory, and call it to get an initialized client in any port, and how many times you want!