姜戈 Pytest 和 Postman 在测试时在单个默认值上得到不同的结果

问题描述

我开始接触 Django 和 pytest。 我创建了一个几乎没有基本组件的项目,并且一切都按设计运行良好。除了 Pytest 结果

模型是:

class Opportunity(models.Model):
class OpportunityQualityLevel(models.TextChoices):
    H = "High"
    M = "Medium"
    L = "Low"

class StageProbability(models.TextChoices):
    A = "Signed Contract"
    B = "Verbal Confirmation"
    C = "Resumes First"
    D = "nothing Firm"

name = models.CharField(max_length=60)
value = models.IntegerField()
quality = models.CharField(max_length=8,choices=OpportunityQualityLevel.choices,default=OpportunityQualityLevel.M,blank=True)
Stage = models.CharField(max_length=24,choices=StageProbability.choices,default=StageProbability.D,blank=True)
client = models.ForeignKey(Client,on_delete=models.CASCADE)
is_active = models.BooleanField(default=True,blank=True)

date_created = models.DateTimeField(auto_Now_add=True,null=True,blank=True)
last_updated = models.DateTimeField(auto_Now=True)

class Meta:
    verbose_name_plural = "Opportunities"

def __str__(self):
    return self.name

序列化程序是:

   class OpportunitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Opportunity
        fields = ["id","name","value","quality","Stage","client","is_active","date_created","last_updated"]

视图是:

class OpportunityViewSet(ModelViewSet):
    queryset = Opportunity.objects.all().order_by("name","-is_active")
    serializer_class = OpportunitySerializer
    pagination_class = PageNumberPagination

网址是:

router = routers.DefaultRouter()
router.register("",views.OpportunityViewSet)

urlpatterns = [
    path("",include(router.urls))
]

测试本身非常简单: 创建客户并从该客户分配机会。值上有问题的字段是“is_active”,我在模型中将其配置为“True”,如模型中所示。

使用 Postman

Postman Test results

进行测试时,Django 中的内置 API 浏览器一切正常。

我为此编写了一个 pytest 测试,但我不断将“is_active”设为错误,我不明白为什么。我尝试在没有客户端的情况下使用简单的“创建”,结果我得到了“真”,只有 pytest 提供了糟糕的结果。

这是 Pytest 代码

def test_create_opportunity_with_name_value_client_success_with_default_values(client) -> None:
    # Setup
    test_client = Client.objects.create(name="Good Client")
    opportunity = {
        "name": "Good Opportunity","value": 27000,"client": test_client.id
    }

    # Run
    response = client.post(path=opportunity_url,data={"name": opportunity["name"],"value": opportunity["value"],"client":opportunity["client"]})
    response_content = json.loads(response.content)
    # print("###### response_content: ",response_content)
    # Assessment
    assert response.status_code == 201
    assert response_content.get("name") == opportunity["name"]
    assert response_content.get("value") == opportunity["value"]
    assert response_content.get("quality") == "Medium"
    assert response_content.get("Stage") == "nothing Firm"
    assert response_content.get("client") == test_client.id
    assert response_content.get("is_active") == True

这是我在使用 Pytest 进行测试时得到的响应。我不知道为什么 'is_active': False 在响应中:

{'id': 1,'name': 'Good Opportunity','value': 27000,'quality': 'Medium','Stage': 'nothing Firm','client': 1,'is_active': False,'date_created': '2021-01-26T05:11:29.329317Z','last_updated': '2021-01-2
6T05:11:29.329317Z'}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)