Monem
Monem

Reputation: 265

how to properly use pytest mark.parametrize decorator when passing arguments for other decorators (e.g fixtures, mocks)?

when I tried to use mark.parametrize along with other decorators as follows:

@pytest.fixture(autouse=True)
def some_fixture()
 db = create_db()
 yield db
 db.close()

@pytest.mark.parametrize('id, expected',[(1,1),(2,2)])
@mock.patch('some_module')
def some_test(mock_module, id, expected, db):
 mock_module.function.return_value = 1
 connection = db.connection()
 assert expected my_function(id, connection)

I have two questions:

Upvotes: 1

Views: 3420

Answers (1)

val.sytch
val.sytch

Reputation: 261

When using patch, parametrize and fixtures altogether, the order is important: Fists should be mention patched values, in reverse order, then parametrized values and fixtures in the end in any order.

@pytest.fixture
def some_fixture1()
    pass

@pytest.fixture
def some_fixture2()
    pass

@pytest.mark.parametrize('id, expected',[(1,1),(2,2)])
@mock.patch('some_module1')
@mock.patch('some_module2')
@mock.patch('some_module3')
def some_test(
   some_module3, 
   some_module2, 
   some_module1, 
   id, 
   expected,
   some_fixture1,
   some_fixture2,
):
    pass

Upvotes: 2

Related Questions