pytest-factoryboy icon indicating copy to clipboard operation
pytest-factoryboy copied to clipboard

Exposing a fixture for deferred factory evaluation

Open youtux opened this issue 1 year ago • 0 comments

It'd be nice to be able to expose a fixture (maybe the factory fixture?) that can be invoked in the test invocation.

Here's a few use cases:

  • creating the object when using a decorator, e.g. factory.django.mute_signals(signals.post_save)
  • create the object imperatively in the test body, for example if you use @pytest.mark.django_db(transactional=True), and you need to create your object within a django.db.transaction.atomic() context manager.

class Book:
    name: str

class BookFactory(Factory):
    name = "foo"


@pytest.mark.parametrize("book__name", ["bar"])
def test_book_initialise_later(book_factory):
    book = book_factory()
    assert book.name == bar

youtux avatar Sep 30 '24 13:09 youtux