私はPytestをDjangoで使用していて、この奇妙な動作になりました。私は2つのユーザーフィクスチャを持っています。一方はもう一方のスーパーセットです。同じテストケースで両方のフィクスチャを使用するまで、すべてが期待通りに機能します。Pytest fixturesが互いに干渉する
備品:最後のテストは明らかにuser_with_password
ので、仕事やuser_without_password
同じオブジェクトになってしまうしません
@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()
。それらが毎回新しいオブジェクトであることを保証する方法はありますか?この動作は直感的ではありません。