什么是Python Django中简单测试的需求

huangapple go评论66阅读模式
英文:

what the need of simple tests in python django

问题

以下是翻译好的代码部分:

我在阅读关于测试的内容时遇到了以下示例

@pytest.fixture
def customer():
    customer = Customer(first_name="Cosmo", last_name="Kramer")
    return customer


def test_customer_sale(customer):
    assert customer.first_name == "Cosmo"
    assert customer.last_name == "Kramer"
    assert isinstance(customer, Customer)

我想知道为什么要测试以下三个条件,如果我们已经知道如何创建对象:

customer.first_name == "Cosmo"
customer.last_name == "Kramer"
isinstance(customer, Customer)
英文:

I was reading about tests and came accross an example as below.

import pytestimport pytest


@pytest.fixture
def customer():
    customer = Customer(first_name="Cosmo", last_name="Kramer")
    return customer


def test_customer_sale(customer):
    assert customer.first_name == "Cosmo"
    assert customer.last_name == "Kramer"
    assert isinstance(customer, Customer)

I am wondering why to test the below three if we we already know how we are creating the object

customer.first_name == "Cosmo"
customer.last_name == "Kramer"
isinstance(customer, Customer)

答案1

得分: 1

作为基本实现测试,它验证代码是否按照预期执行。如果在Customer实现中进行了粗心的更改,可能会导致在某些情况下丢失名字,例如。这有助于避免后续更复杂的失败,这些失败假设基本功能按预期工作。

英文:

As a basic implementation test, it verifies that the code does what it's supposed to do. If you made a careless change in the Customer implementation, maybe it would lose the first name in some circumstances, for example. This helps avoid more complex failures down the line from tests which assume that the basic functionality is working as expected.

答案2

得分: 0

你不会这样做。如果这些本来是要成为真正的单元测试,那它们就是糟糕的单元测试。但很可能它们只是用来演示如何使用pytest,而不是如何编写好的单元测试。

为什么这是糟糕的单元测试?因为它是一个测试一个方法的测试,实际上什么都没做,只是测试了1) 构造函数(另一个方法),和 2) Python 的赋值功能是否有效。如果你想测试构造函数(如果它有非平凡的逻辑,这并不是个坏主意),你应该为它编写专门的测试。但单元测试应该只测试它们为之编写的方法,而不是被测试方法调用的方法(这些方法应该被模拟掉)。它们应该测试非平凡的功能。那些短小简单,只需查看即可轻松验证的函数(例如,只是赋值一个值的函数)不需要单元测试。无聊的单元测试浪费开发时间,并给出了虚假的安全感。

英文:

You wouldn’t. If those were meant to be real unit tests they would be bad unit tests. But most likely they were just meant to demonstrate how to use pytest and not how to write good unit tests.

Why is it a bad unit test? Because it’s a test for one method that is effectively doing nothing more that testing 1) the constructor (another method), and 2) that the assignment feature of Python works. If you want to test the constructor (not a bad idea if it has non-trivial logic), you should write tests specifically for it. But unit tests should only test the method they’re written for, not methods called by the method under test (those should be mocked out). And they should test non-trivial functionality. Functions that are short and simple enough to be easy to validate by just looking at them (e.g. functions that just assign a value) don’t need unit tests. Trivial unit tests waste development time and give a false sense of security.

huangapple
  • 本文由 发表于 2023年5月17日 11:01:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定