对于django项目,我有一个有效的有效测试,dummy_book
和test_profile
都是固定装置。注意它有两个断言:
def test_correct_items_in_feed(client, dummy_book, test_profile):
some_author = dummy_book.author_set.first()
# profile subscribes to author
Subscription.attempt_addition_by_id(test_profile, some_author.vendor_id)
client.login(username=test_profile.user.username, password="somepassword")
test_profile.save()
# wants fiction books, feed requested
requested_url = reverse("personal_feed", args=[test_profile.auth_token])
resp = client.get(requested_url, follow=True)
assert dummy_book.title in str(resp.content)
# user no longer wants fiction books, feed requested
test_profile.notify_fiction = False
test_profile.save()
resp = client.get(f"/users/feeds/{test_profile.auth_token}", follow=True)
assert dummy_book.title not in str(resp.content)
我现在想要实现的是一种测试两个不同的test_profile
的方法:
一个test_premium_profile
,它可以按类型过滤Book
(请注意模型属性test_profile.notify_fiction
)
另一个test_free_profile
不允许按类型过滤Book
s
我已经有了以上所述的逻辑,只是忽略了test_free_profile
关于此属性的偏好。
这里的挑战来自以下事实:对于这两个不同的配置文件,我们需要声明不同的断言。具体来说,在两种情况下,对于test_free_profile
dummy_book.title
应该在resp.content
中,而对于test_premium_profile
,它应该仅在第一种情况下存在。
如何在不重复此测试及其代码的情况下为两种Profile
安排这些不同的断言?
我知道一个更简单的解决方案是将测试分为两部分,但是我对此并不感兴趣,出于教育的考虑,这是不可能的。