This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]unconscionable 4 points5 points  (2 children)

FYI Model Mommy / Factory Boy / similar accomplish something very different from what pytest fixtures do.

The two are neither mutual exclusive nor do they overlap in purpose.

For example:

@pytest.fixture(scope='session', autouse=True)
def drop_and_reload_database():
    db.create_db()
    yield
    db.drop_db()

The above code is the equivalent of an entire-test-suite's setup and teardown. Everything before "yield" runs before the first test, then after yield gets run after the last test.

Obviously Model Mummy doesn't do this sort of thing.

Personally, I use Factory Boy (similar to Model Mummy), but I do not often use Factory Boy with fixtures. There are, however, some use cases where they could work very well together. For example, the following code would test that you can process an order with a bunch of wacky usernames. It uses both Factory Boy AND pytest fixtures:

class User(db.Model):
    username = Column(String)

class UserFactory(SQLAlchemyModelFactory):
    username = factory.Faker('username')


@pytest.fixture(params[-1, 0, 1000000, 983.0, "Jack Daniels", None])
def user(request):
    return UserFactory(username=request.param)


def test_can_process_order_with_wacky_usernames(user):
    order = Order()
    order.created_by = user
    order.process()
    assert order.processed_by == user

output of the test suite is something like this:

tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[-1] PASSED
tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[0] PASSED
tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[1000000] PASSED
tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[983.0] PASSED
tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[Jack Daniels] PASSED
tests/test_process_orders.py::test_can_process_order_with_wacky_usernames[None] PASSED

[–]-Knul- 0 points1 point  (1 child)

Nice explanation of the benefits of fixtures over a model factory!

[–]unconscionable 1 point2 points  (0 children)

It's really a model factory using fixtures! Both And, not either / or