简体   繁体   English

Python unittest计算测试数量

[英]Python unittest counting the number of tests

It's my first time playing around with Python's unittest for an assignment in school. 这是我第一次使用Python的单元测试在学校进行作业。 I basically have a Circle Object, where I am using pyunit to make sure the data is stored properly. 我基本上有一个Circle对象,在这里我使用pyunit来确保数据正确存储。

I noticed that Python only counts the number of methods as test cases as opposed to the number of assert statements. 我注意到,Python仅将方法的数量视为测试用例,而不是assert语句的数量。

For instance I want to test that methods are working correctly, Python only counts the following as 2 tests, despite having 4 assert statements. 例如,我想测试方法是否正常工作,尽管有4条assert语句,Python却仅将以下内容计为2次测试。 It really caught me off guard, as with Java's JUnit it will count the number of assert statements instead. 确实让我措手不及,因为使用Java的JUnit,它将改为计算assert语句的数量。

def test_xcrd(self): 
    self.assertTrue(self.point.xcrd() == 1) 
    self.assertFalse(self.point.xcrd() == 5)

def test_ycrd(self): 
    self.assertTrue(self.point.ycrd() == 2) 
    self.assertFalse(self.point.ycrd() == 10)

What's the "norm" in python? python中的“规范”是什么? Should there only be one assert statement per method? 每个方法应该只有一个assert语句吗?

Python's unittest package allows you to structure your unit tests in separate methods like you are noticing. Python的unittest包允许您像注意到的那样用单独的方法构造单元测试。 This is useful in cases where you want to tests things which are very closely related and don't require separate unit tests. 如果您要测试非常相关的事物并且不需要单独的单元测试,这将很有用。

unittest tests start by subclassing unittest.Test , and then adding methods to this. unittest的测试通过继承开始unittest.Test ,然后添加方法这一点。 So, you can add several layers separation between different unittests which are more less related. 因此,您可以在相关性较低的不同单元测试之间添加几层分隔。

An example from the Python Docs demonstrates what is considered to be best practice for Python unit tests: Python Docs中的一个示例演示了Python单元测试的最佳实践:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()

There are a number of things you can observe here: 您可以在这里观察到很多事情:

  1. The three methods of TestStringMethods are the separate unittests. TestStringMethods的三种方法是单独的单元测试。
  2. test_isupper and test_split both contain two asserts, since they are very closely related. test_isuppertest_split都包含两个断言,因为它们关系非常密切。 Adding separate tests for the two asserts present in test_isupper would be adding lots of bloat to the code, and it could lead to very weird problems. test_isupper存在的两个断言添加单独的测试会给代码增加很多膨胀,并且可能导致非常奇怪的问题。

For example, if str.isupper() would break in a weird way, the single unittest covering this single function would break. 例如,如果str.isupper()以一种奇怪的方式中断,则覆盖此单个函数的单个单元测试将中断。 However, if the two tests for "FOO" and "Foo" were separate, one test might pass, and the other fail. 但是,如果针对"FOO""Foo"的两个测试是分开的,则一个测试可能会通过,而另一个则失败。 So, testing the functionality of a single function is better kept in a single unittest with several asserts. 因此,测试单个功能的功能最好保留在具有多个断言的单个单元测试中。

The same applies to the test_split method; 同样适用于test_split方法; checking that str.split() works and checking that it raises a TypeError are closely related, and should therefore best be kept close together in code as well. 检查str.split()有效以及检查它是否引发TypeError是密切相关的,因此最好在代码中将它们保持在一起。

So, to come back to your question: There can (and sometimes should) be more than one assert per method, since it leads to simpler and clearer code, and less confusion. 因此,回到您的问题:每个方法可以(有时应该)有多个断言,因为它可以导致代码更简单明了,并且混乱更少。 To quote the "Zen of Python" (found by running import this in a python shell): "Simple is better than complex". 引用“ Zen of Python”(通过在Python shell中运行import this找到import this ):“简单胜于复杂”。 So, keep your unittests simple and structured by grouping similar asserts in a single method. 因此,通过在一个方法中将相似的断言分组,可以使单元测试变得简单和结构化。

The answer to your question '''What's the "norm" in python? 您的问题的答案'''python中的“规范”是什么? Should there only be one assert statement per method?''' is "No". 每个方法应该只有一个assert语句吗?'''是“ No”。 Some people might say 'yes' but CPython core developers, including me, routinely use multiple asserts in test methods. 有人可能会说“是”,但是包括我在内的CPython核心开发人员通常在测试方法中使用多个断言。 Take a look at the test_xyz files in Lib/test (if your installation includes that directory). 查看Lib/test中的test_xyz文件(如果您的安装包括该目录)。

It is true that one method should test one unit or even one behavior of one unit. 的确,一种方法应该测试一个单元甚至一个单元的一种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM