Skip to content

5.3.3 fixture order wrong with 'trylast' #6540

Closed
@lhupfeldt

Description

@lhupfeldt

Test fails randomly during setup because of bad fixture order after upgrading to 5.3.3.
A fixture marked 'trylast' is being called too early.
It fails randomly only about 50% of the time.
Test run stable with 5.3.2 and 5.3.4.

Example attached (reduced from some complex database setup).
Note that the example also uses dynamically generated fixtures, I'm not sure if this is part of the issue.
fixture_order.zip

Output from a run failed because of fixture called too early:

-*- mode: compilation; default-directory: "~/src/fixture_order/" -*-
Compilation started at Wed Jan 22 23:46:53

nox
nox > Running session fixture_order-3.7
nox > Re-using existing virtual environment at .nox/fixture_order-3-7.
nox > pip install pytest==5.3.3
nox > pip freeze
attrs==19.3.0
importlib-metadata==1.4.0
more-itertools==8.1.0
packaging==20.0
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.6
pytest==5.3.3
six==1.14.0
wcwidth==0.1.8
zipp==2.0.0
nox > pytest 
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.7.6, pytest-5.3.3, py-1.8.1, pluggy-0.13.1
rootdir: /home/lhn/src/fixture_order, inifile: pytest.ini, testpaths: test
collected 1 item                                                                                                                                                                

test/order_test.py E                                                                                                                                                      [100%]

==================================================================================== ERRORS =====================================================================================
______________________________________________________________ ERROR at setup of test_using_try_last_local_fixture ______________________________________________________________

conn = <conftest.Connection object at 0x7f1af6443f10>, table = 'dummy_table'

    def copy_csv_file(conn, table):
        """Copy a csv file to a table."""
    
        print(f'DUMMY copy_csv_file(({conn}, {table})')
        if conn.get_dsn_parameters()['dbname'] in _TRIGGERS_CREATED:
>           raise Exception("Oops wrong fixture order, 'copy_csv_file' called after 'create_triggers'!")
E           Exception: Oops wrong fixture order, 'copy_csv_file' called after 'create_triggers'!

test/conftest.py:46: Exception
----------------------------------------------------------------------------- Captured stdout setup -----------------------------------------------------------------------------
DUMMY - session autouse2 - seems to increase likelyhood of wrong order?
fixture(session): test_db_server
DUMMY create schema and load functions, roles, types
DUMMY clean_db_cli_runner(<conftest.Connection object at 0x7f1af6443ad0>)
Create triggers because test 'test_using_try_last_local_fixture' uses database connection.
DUMMY create_triggers(<conftest.Connection object at 0x7f1af6443f10>)
Executing table load fixture: dbdata1
DUMMY copy_csv_file((<conftest.Connection object at 0x7f1af6443f10>, dummy_table)
=============================================================================== 1 error in 0.03s ================================================================================

Output from run using 5.3.4 with correct fixture invocation order (explicitly failed to show setup):

-*- mode: compilation; default-directory: "~/src/fixture_order/" -*-
Compilation started at Wed Jan 22 23:40:13

nox
nox > Running session fixture_order-3.7
nox > Re-using existing virtual environment at .nox/fixture_order-3-7.
nox > pip install pytest==5.3.4
nox > pip freeze
attrs==19.3.0
importlib-metadata==1.4.0
more-itertools==8.1.0
packaging==20.0
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.6
pytest==5.3.4
six==1.14.0
wcwidth==0.1.8
zipp==2.0.0
nox > pytest 
============================================================================== test session starts ==============================================================================
platform linux -- Python 3.7.6, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: /home/lhn/src/fixture_order, inifile: pytest.ini, testpaths: test
collected 1 item                                                                                                                                                                

test/order_test.py F                                                                                                                                                      [100%]

=================================================================================== FAILURES ====================================================================================
_______________________________________________________________________ test_using_try_last_local_fixture _______________________________________________________________________

clean_db_cli_runner = None, _local_fixture_requiring_the_same_code_as_hook_called_now = None

    def test_using_try_last_local_fixture(
            clean_db_cli_runner, _local_fixture_requiring_the_same_code_as_hook_called_now):
>       assert False
E       assert False

test/order_test.py:12: AssertionError
----------------------------------------------------------------------------- Captured stdout setup -----------------------------------------------------------------------------
DUMMY - session autouse2 - seems to increase likelyhood of wrong order?
fixture(session): test_db_server
DUMMY create schema and load functions, roles, types
DUMMY clean_db_cli_runner(<conftest.Connection object at 0x7ff6bf295450>)
Executing table load fixture: dbdata1
DUMMY copy_csv_file((<conftest.Connection object at 0x7ff6bf2953d0>, dummy_table)
Executing table load fixture: dbdata2
DUMMY copy_csv_file((<conftest.Connection object at 0x7ff6bf2953d0>, dummy_table)
Executing table load fixture: dbdata3
DUMMY copy_csv_file((<conftest.Connection object at 0x7ff6bf2953d0>, dummy_table)
Create triggers because test 'test_using_try_last_local_fixture' uses database connection.
DUMMY create_triggers(<conftest.Connection object at 0x7ff6bf2953d0>)
DUMMY some setup code which requires the create_register_triggers fixture to have been called...
Running setup hook
Setup hook: 'create_triggers' because of fixture 'test_db_server'
Create triggers because test 'postgres' uses database connection.
DUMMY create_triggers(<conftest.Connection object at 0x7ff6bf295410>)
--------------------------------------------------------------------------- Captured stdout teardown ----------------------------------------------------------------------------
DUMMMY close <conftest.Connection object at 0x7ff6bf2953d0>
fixture(session): test_db_server - finalizer
=============================================================================== 1 failed in 0.02s ===============================================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions