reach jaango
reach jaango

Reputation: 67

getting fixture from parameterized for multiple tests

my code

@pytest.mark.parametrize(
    ("username", "password", "message"),
    (
        ("", "", b"Username is required."),
        ("a", "", b"Password is required."),
        ("test", "test", b"already registered"),
    ),
)
def test_register_validate_input(client, username, password, message):
    response = client.post(
        "/register", data={"username": username, "password": password}
    )
    assert message in response.data

def test_register_delete_input(client, username, password, message):
    response = client.delete(
        "/register", data={"username": username, "password": password}
    )
    assert message in response.data

if i run python -m pytest i am getting

fixture 'username' not found

however if i remove the block for

def test_register_delete_input(client, username, password, message):
    response = client.delete(
        "/register", data={"username": username, "password": password}
    )
    assert message in response.data

the test works

  1. How can i use the fixture in the delete test as well?
  2. the difference is only with post/delete in the two tests. Is there a way to combine?

Upvotes: 0

Views: 36

Answers (1)

Mariya Fetishcheva
Mariya Fetishcheva

Reputation: 71

  1. You could copypaste the @pytest.mark.parametrize(...) block from the first test to the second test, because it only parametrizes the first test but not the second. Alternatively, do what this user recommended here - getting fixture from parameterized for multiple tests

  2. It could be combined, in theory; but, since you're testing two different actions (post in first one and delete in the second one), I'd say it's better to keep these two tests separate.

Upvotes: 1

Related Questions