0

I defined UserFactory class in tests/factories.py as shown below following the doc. *I use pytest-django and pytest-factoryboy in Django:

# "tests/factories.py"

import factory

from django.contrib.auth.models import User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

And, I defined test_user_instance() with @pytest.mark.parametrize() to test 4 users as shown below:

# "tests/test_ex1.py"

import pytest
from django.contrib.auth.models import User

@pytest.mark.parametrize(
    "username, email",
    {
        ("John", "test@test.com"), # Here
        ("John", "test@test.com"), # Here
        ("John", "test@test.com"), # Here
        ("John", "test@test.com")  # Here
    }
)
def test_user_instance(
    db, user_factory, username, email
):
    user_factory(
        username=username,
        email=email
    )

    item = User.objects.all().count()
    assert item == True

But, only one user was tested as shown below:

$ pytest -q
.                                [100%]
1 passed in 0.65s

So, how can I test 4 tests?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • I'm voting to close this question as typo. You can't repeat `("John", "test@test.com")` (a tuple/hashable) 4 times inside a set and expect to get all 4 back. – InSync Aug 15 '23 at 04:40

1 Answers1

0

You should use [] instead of {} to surround 4 users as shown below:

# "tests/test_ex1.py"

import pytest
from django.contrib.auth.models import User

@pytest.mark.parametrize(
    "username, email",
    [ # <- Here
        ("John", "test@test.com"),
        ("John", "test@test.com"),
        ("John", "test@test.com"),
        ("John", "test@test.com")
    ] # <- Here
)
def test_user_instance(
    db, user_factory, username, email
):
    user_factory(
        username=username,
        email=email
    )

    item = User.objects.all().count()
    assert item == True

Then, you can test 4 users as shown below:

$ pytest -q
....                             [100%]
4 passed in 0.68s
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129