pytest-dev / pytest-dev/pytest-django

Saving of _factory doesn't apear, if run few test at once

Open
#1,054 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

Hi! I fetched strange behaviour, most probably the bag, that I resolved, but it shouldn't be. That you can see under a comments

@pytest.fixture()
def hundred_provider_stocks(provider_stock_factory):
    provider_stock_factory.create_batch(10)


@pytest.mark.django_db
@pytest.mark.parametrize('browser', [lazy_fixture("partner_browser_client"), lazy_fixture("client")])
@pytest.mark.parametrize('page', [('products-detail', [1]), ('products-list', [])])
def test_get_serializer_class(browser, page, hundred_provider_stocks):
    page = browser.get(reverse(page[0], args=page[1]))
    assert page.status_code == 200


@pytest.mark.django_db(transaction=True)
def test_notify_about_providerstock_change(provider_stock_factory, demand_factory, personal_subscription_factory,
                                           product, mailoutbox, celery_worker):
    d = demand_factory(individual_demands=[product])
    personal_subscription_factory(demand=d)
    p = provider_stock_factory(product=product)
    p.save() # If I don't type this, mail, that should be sent each time, when object is changing, is not sending, if I run all test at one time in module. So I have only 1 mail in my mailoutbox. But it works good if I run only test_notify_about_providerstock_change. 
    p.price = 1000
    p.save()
    logging.info(mailoutbox[0].body)
    assert len(mailoutbox) == 2

And second question is that I wanted to do @pytest.fixture(scope="session" or "module") on hundred_provider_stocks but I got error

ScopeMismatch: You tried to access the function scoped fixture provider_stock_factory with a module scoped request object, involved factories:
goods\test_app.py:13:  def hundred_provider_stocks(provider_stock_factory)
venv\lib\site-packages\pytest_factoryboy\fixture.py:493:  def fn(request: 'SubRequest', *, factory_class: 'F' = <class 'keysbilling.factoryboy_fixtures.ProviderStockFactory'>) -> 'F'

This solution doesn't work for me
https://stackoverflow.com/questions/51783757/pytest-scopemismatch-error-how-to-use-fixtures-properly

Contributor guide

Open the contributing guide

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

Start with goods\test_app.py, especially test_get_serializer_class and test_notify_about_providerstock_change, and reproduce them together and individually. Compare the fixture-scope failure with the provider_stock_factory traceback in site-packages\pytest_factoryboy\fixture.py; done means isolating whether the mail behavior and ScopeMismatch are pytest-django issues and defining the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.