简体   繁体   English

我如何才能团结一致地只找到一次我的测试方法?

[英]How can I make unitest to find my single test method only once?

I have a python unitest script with an double inheritance of the TestCase as follows: 我有一个带有TestCase双重继承的python unitest脚本,如下所示:

import unittest
class Upper(unittest.TestCase):
    def test_dummy(self):
        pass

class Lower(Upper):
    pass

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

The idea is to define an upper class with a test method implementation, and derive from this class (in different subdirectories) which contain some additional setup functionality. 这个想法是用测试方法实现定义一个上层类,并从这个类派生出来(在不同的子目录中),其中包含一些附加的设置功能。 In the end, there is one upper.py from which many different test_lower.py are derived. 最后,有一个upper.py ,从中派生出许多不同的test_lower.py The test methods are ONLY implemented in upper.py . 测试方法仅在upper.py实现。

Given the example above now I do a python test_example.py only to see that python is trying to run 2 tests! 鉴于上面的示例,我现在执行一个python test_example.py只是为了看到python试图运行2个测试! The script contains exactly one test, so why is unittest executing two tests? 该脚本仅包含一个测试,那么为什么unittest执行两个测试?

I assume that unittest finds one test within Lower and one test in Upper somehow, but I only want to execute the test which is found in Lower (because of additional and required setup functionalities). 我假设unittest可以在Lower找到一个测试,而在Upper找到一个测试,但是我只想执行在Lower找到的测试(因为附加的和必需的设置功能)。 How can I achieve this? 我该如何实现?

Context In the real case the two classes are defined in two different files, residing in two directories. 上下文在实际情况下,这两个类在两个不同的文件中定义,分别位于两个目录中。 Maybe this helps. 也许这会有所帮助。

Unittest library iterates over a TestCase subclass attributes, finding methods which start with test_ . Unittest库遍历TestCase子类属性,查找以test_开头的方法。 In case of Lower test case it inherits a method from its parent, so it is executed twice - once inside Upper test case and second time inside Lower class. Lower测试用例的情况下,它从其父级继承了一个方法,因此它执行两次-一次在Upper测试用例内部,第二次在Lower类内部。 If both of the test cases are meant to be run, the only solution i see is to make a take out test_dummy test to an other subclass of Upper 如果两个测试用例都打算运行,那么我看到的唯一解决方案是对Upper的另一个子类进行test_dummy测试

If you want a test to be run in a parent class, but skipped in some of its subclasses, try this: 如果您希望测试在父类中运行,但跳过了某些子类,请尝试以下操作:

import unittest
class Upper(unittest.TestCase):
    def test_dummy(self):
        pass

class Lower(Upper):
    def test_dummy(self):
        return  # skip test

    @unittest.skip  # or this - but ugly
    def test_dummy(self):
        return  # skip test

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

UPDATE: 更新:

I think now i understand what you want: you want the test method to be run only in subclasses. 我想现在我明白了您想要什么:您希望测试方法仅在子类中运行。 Then i suggest you to inherit Upper from object , and the subclasses - both from Upper and TestCase : 然后我建议您从object继承Upper ,并从UpperTestCase继承子类:

import unittest

class Upper(object):

    def test_dummy(self):
        self.assertTrue(True)

class Lower(unittest.TestCase, Upper):

    pass

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

Running: 正在运行:

python test2.py -v
test_dummy (__main__.Lower) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

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

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