question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong fixture/conftest being used

See original GitHub issue

I discovered a weird issue when running int tests on our Jenkins node at work, which I cannot reproduce inside a dev/vagrant VM using the same versions. The behaviour described below is only on the Jenkins node, but I don’t really know where to go next in working out why it’s happening. The original errors were in a way more complex setup, but I’ve managed to reduce this to a fairly minimal example. I doubt anyone else will be able to repro this in isolation, but i’m hoping someone can help me with where to look next to work out what’s going on.

Minimal example file structure:

integration_tests/init.py (empty)

integration_tests/aa/init.py (empty)

integration_tests/aa/conftest.py

import pytest
@pytest.fixture
def url():
    return "1"

integration_tests/aa/test_x.py

def test_url(url):
    assert url == "1"

integration_tests/aa_foo/init.py (empty)

integration_tests/aa_foo/conftest.py

import pytest
@pytest.fixture
def url():
    return "2"

integration_tests/aa_foo/test_x.py

def test_url(url):
    assert url == "2"

So we have 2 packages, each containing a conftest defining a fixture url and each containing a test.

07:51:50 py.test integration_tests/
07:51:50 ============================= test session starts ==============================
07:51:50 platform linux -- Python 3.5.1, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
07:51:50 rootdir: /var/lib/jenkins/workspace/adminweb-int-test, inifile:
07:51:50 collected 2 items
07:51:50 
07:51:50 integration_tests/aa/test_x.py .
07:51:50 integration_tests/aa_foo/test_x.py F
07:51:50 
07:51:50 =================================== FAILURES ===================================
07:51:50 ___________________________________ test_url ___________________________________
07:51:50 
07:51:50 url = '1'
07:51:50 
07:51:50     def test_url(url):
07:51:50 >       assert url == "2"
07:51:50 E       AssertionError: assert '1' == '2'
07:51:50 E         - 1
07:51:50 E         + 2
07:51:50 
07:51:50 integration_tests/aa_foo/test_x.py:3: AssertionError
07:51:50 ====================== 1 failed, 1 passed in 0.04 seconds ======================

What we see is that when aa_foo/test_x.py executes, it’s using the wrong url fixture - it’s using the fixture from aa rather than aa_foo. From investigation, it seems to be somehow related to aa and aa_foo sharing a common root name (aa). Longer versions of this name (originally these dirs were named create and create_with_stuff shows the same issue. I shortened the name repeatedly, and aa is the sortest name that shows the issue - if I change the package names to a and a_foo, it starts working again.

As said at the start, it works fine in a local development (vagrant) VM. Both cases (broken jenkins and working dev VM) are running Ubuntu 14.04 and python 3.5, and I’ve also created a new virtual env that only contains/installs py.test 3.2.3 to rule out other packages affecting this.

Are there any known bugs/issues in pytest that can cause this sort of behaviour? The original issue was found in a complex nest of parameterised fixtures defined at different levels, and I had originally assumed the bug was in that nest, but distilling it down to this simple example rules all the complexity out. It seems like pytest is simply using the wrong fixture.

Any help or guidance anyone can provide getting to the bottom of this would be very much appreciated!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
tom-dalton-fanduelcommented, Oct 23, 2017

I think I’ve found the cause of my woes, but I’ll need someone to confirm if this is expected behaviour or a bug. I suspect (hope) it’s the latter!

The problem seems to come down to this method: https://github.com/pytest-dev/pytest/blob/master/_pytest/fixtures.py#L1133

which:

  • takes a list of possible fixtures and a node (test file) where the fixture name is used
  • returns the list of fixtures that the node can ‘see’

The way it decides if the node can see the fixture is a simple nodeid.startswith(fixturedef.baseid).

This means that in the case we have a test node like integration_tests/aa_foo/test_x.py, then it (I think incorrectly) thinks that the fixture from integration_tests/aa is a match - even though in fact this is a sibling package, rather than a parent package.

I’ll make a minimal example repo to show the behaviour.

0reactions
tom-dalton-fanduelcommented, Oct 24, 2017

Closing - Fixed by #2862

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixture not found pytest error is being displayed when I run ...
Pytest use a single file for fixture discovery, the conftest.py file. You should then store the fixtures declaration in a conftest.py file ...
Read more >
How to use fixtures
In this example, the append_first fixture is an autouse fixture. Because it happens automatically, both tests are affected by it, even though neither...
Read more >
Pytest - Fixtures
Pytest - Fixtures, Fixtures are functions, which will run before each test function to which it is applied. Fixtures are used to feed...
Read more >
Pytest Tutorial: Sharing Fixtures Through conftest - YouTube
You can use fixtures to get a data set for the tests to work on. ... The conftest.py file gets read by pytest,...
Read more >
Effective Python Testing With Pytest
assertTrue(False) AssertionError: False is not true ... Another interesting use case for fixtures and conftest.py is in guarding access to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found