Python unittest 运行 多次

Python unittest running multiple times

tldr;我的单元测试 运行ning 两次。知道为什么吗?

假设我有 2 个文件:

a.py
checker.py

我对 a.py 有一个奇怪的要求。 a.py有以下代码,要求有此代码:

import checker

def factorial(n):
    if n == -1:
        raise Exception("Raised this exception on purpose")
    result = 1
    for i in range(1,n+1):
        result *= i

    return result

total = 53

checker.runTests()

我需要能够 运行 checker.py

中的阶乘测试
import unittest


class MyTestCase(unittest.TestCase):
    def test_factorial3(self):
        import a
        self.assertEqual(a.factorial(3), 6)

    def test_factorial4(self):
        import a
        self.assertEqual(a.factorial(4), 24)

    def test_factorial5(self):
        import a
        self.assertEqual(a.factorial(5), 120)

    def test_factorialnegative1(self):
        import a
        self.assertEqual(a.factorial(-1), 0)

    def test_total(self):
        import a
        self.assertEqual(a.total, 53)
def runTests():
    runner = unittest.TextTestRunner(verbosity=2)
    suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
    runner.run(suite)

如果有任何更好的方法来处理循环依赖,我愿意接受有关如何改进 checker.py 的建议。但是,我的问题是当我 运行 a.py 时,我没有得到预期的输出,特别是测试是 运行 两次?参见:

test_factorial3 (cisc106checker.MyTestCase) ... test_factorial3 (cisc106checker.MyTestCase) ... ok test_factorial4 (cisc106checker.MyTestCase) ... ok test_factorial5 (cisc106checker.MyTestCase) ... ok test_factorialnegative1 (cisc106checker.MyTestCase) ... ERROR test_total (cisc106checker.MyTestCase) ... ok

====================================================================== ERROR: test_factorialnegative1 (cisc106checker.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/tnj/PycharmProjects/AutoExam/cisc106checker.py", line 21, in test_factorialnegative1 self.assertEqual(x.factorial(-1), 0) File "/Users/tnj/PycharmProjects/AutoExam/mattsapQ16.py", line 10, in factorial raise Exception("Matt Raised this exception on purpose") Exception: Matt Raised this exception on purpose

---------------------------------------------------------------------- Ran 5 tests in 0.001s

FAILED (errors=1) ok test_factorial4 (cisc106checker.MyTestCase) ... ok test_factorial5 (cisc106checker.MyTestCase) ... ok test_factorialnegative1 (cisc106checker.MyTestCase) ... ERROR test_total (cisc106checker.MyTestCase) ... ok

====================================================================== ERROR: test_factorialnegative1 (cisc106checker.MyTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/tnj/PycharmProjects/AutoExam/cisc106checker.py", line 21, in test_factorialnegative1 self.assertEqual(x.factorial(-1), 0) File "/Users/tnj/PycharmProjects/AutoExam/mattsapQ16.py", line 10, in factorial raise Exception("Matt Raised this exception on purpose") Exception: Matt Raised this exception on purpose

---------------------------------------------------------------------- Ran 5 tests in 0.005s

FAILED (errors=1)

但是,当我从 checker.py 运行 时,它只有 运行 一次。到底是怎么回事?为什么 运行ning 两次?

你之外的片段在a.py

中发挥作用
total = 53 
checker.runTests()

在 运行 a.py 时执行一次,然后在 checker.py 中导入时再次执行。所以 runTests() 被执行了两次。

简短版:使用解决方案去掉文件开头的 import checker 和文件末尾的 checker.runTests()

更长的版本:

通过调用 "production code" 中的测试,您可以将代码附加到测试中。而且你真的不想这样做。

对于 运行 你的测试,你可以将测试文件的末尾更改为

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

和 运行 通过调用 python my_test_file.py.

进行测试

按照简短版本中描述的步骤也将解决您提到的循环依赖问题。

另一个容易做的改进:你可以在第二行写import a,去掉所有重复的import a行。

也许更好的办法是问,"What is the best scaffolding I can give to my students so it's easy for them to write and run tests?" 他们将同时编辑产品文件 (a.py) 和测试文件 (checker.py),所以为什么选择一个在另一个之上?让他们 运行 checker.py 到 运行 进行测试。那么你就不会引入导入循环,它们将有一个他们可以理解的简单结构。