0
我在Django中使用了Pytest,并发现了这种奇怪的行为。我有两个用户装置,一个是另一个的超集。一切工作如预期,直到我在同一个测试用例中使用两个灯具。Pytest夹具互相干扰
灯具:
@pytest.fixture
def user_without_password():
return User.objects.create_user(username=fake.name(), email=fake.email())
@pytest.fixture
def user_with_password(user_without_password):
user = user_without_password
user.set_password('topsecret')
user.save()
return user
测试
@pytest.mark.django_db()
def test_without_pass(user_without_password):
assert not user_without_password.has_usable_password()
@pytest.mark.django_db()
def test_with_pass(user_with_password):
assert user_with_password.has_usable_password()
# THIS FAILS!!
@pytest.mark.django_db()
def test_both(user_with_password, user_without_password):
assert not user_without_password.has_usable_password()
assert user_with_password.has_usable_password()
最后一次测试,因为显然user_with_password
不工作,user_without_password
最终被同一个对象。有没有办法确保每次都是新对象?这种行为感觉违反直觉。