pytest-dev / pytest-dev/pytest-factoryboy

PytestAssertRewriteWarning

Open
#187 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
400
Forks
44
Avg merge
6h 34m
Merged PRs (30d)
1

Description

Hello there,
When I run my tests I got the following warning

../../usr/local/lib/python3.11/site-packages/_pytest/config/init.py:747
/usr/local/lib/python3.11/site-packages/_pytest/config/init.py:747: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: products.tests.factories
self.import_plugin(import_spec)

This happens because I have one file of fixtures

@register
class ProductCycleFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = ProductCycle

    product = factory.SubFactory(ProductFactory)
    cycle_id = factory.Faker("numerify", text="#" * 9)
    vat_id = factory.Faker("numerify", text="#" * 9)

And in my other file of fixtures i have

from products.tests.factories import ProductCycleFactory

@register
class InvoiceLineFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = InvoiceLine

    description = factory.Faker("sentence", nb_words=3)
    invoice = factory.SubFactory(InvoiceFactory)
    product_cycle = factory.SubFactory(ProductCycleFactory)

I already try to change to product_cycle = factory.SubFactory("products.tests.factories.ProductCycleFactory")

My requirements

pytest==7.2.0
pytest-django==4.5.2
pytest-factoryboy==2.5.1

Any idea how I solve this warning,
Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with pytest==7.2.0, pytest-django==4.5.2, and pytest-factoryboy==2.5.1, starting from the products.tests.factories imports and the pytest config warning at _pytest/config/init.py:747. Compare the direct ProductCycleFactory import with the string-based SubFactory reference. Done means the fixture setup runs without PytestAssertRewriteWarning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.