我的测试文件中包含以下代码,并尝试对其进行重构。我是pytest的新手,我正在尝试实现与unittest相似的setUp方法,以便能够将在db中创建的对象检索到其他函数,而不必重复代码。
在这种情况下,我想将 month 从 test_setup 复用到其他功能。
test_models.py
@pytest.mark.django_db
class TestMonth:
# def test_setup(self):
# month = Month.objects.create(name="january",slug="january")
# month.save()
def test_month_model_save(self):
month = Month.objects.create(name="january",slug="january")
month.save()
assert month.name == "january"
assert month.name == month.slug
def test_month_get_absolute_url(self,client):
month = Month.objects.create(name="january",slug="january")
month.save()
response = client.get(reverse('core:month_detail',kwargs={'slug': month.slug}))
assert response.status_code == 200
我会很感激。