Python 从其他文件运行单元测试

Python 从其他文件运行单元测试,python,python-unittest,Python,Python Unittest,我有一个包含unittests的文件TestProtocol.py。我可以按预期运行该脚本并获得30个测试的测试结果。现在,我想从位于同一目录中的另一个文件tester.py运行这些测试。在tester.py内部,我尝试了importtestprotocol,但它运行0个测试 然后我找到了文件,上面说我应该这样做: suite = unittest.TestLoader().discover(".", pattern = "*") unittest.run(suite) 这应该检查当前目录中与

我有一个包含unittests的文件
TestProtocol.py
。我可以按预期运行该脚本并获得30个测试的测试结果。现在,我想从位于同一目录中的另一个文件
tester.py
运行这些测试。在
tester.py
内部,我尝试了
importtestprotocol
,但它运行0个测试

然后我找到了文件,上面说我应该这样做:

suite = unittest.TestLoader().discover(".", pattern = "*")
unittest.run(suite)
这应该检查当前目录
中与模式
*
匹配的所有文件,以便在所有文件中执行所有测试。不幸的是,它再次运行0个测试

有一种方法建议这样做

import TestProtocol
suite = unittest.findTestCases(TestProtocol)
unittest.run(suite)
但这也没有发现任何测试


如何导入和运行测试?

您可以尝试以下方法

# preferred module name would be test_protol as CamelCase convention are used for class name
import TestProtocol

# try to load all testcases from given module, hope your testcases are extending from unittest.TestCase
suite = unittest.TestLoader().loadTestsFromModule(TestProtocol)
# run all tests with verbosity 
unittest.TextTestRunner(verbosity=2).run(suite)
这里有一个完整的例子

文件1:test_me.py

# file 1: test_me.py 
import unittest

class TestMe(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

if __name__ == '__main__':
    unittest.main()
文件2:test_other.py,将其放在同一目录下

# file 2: test_other.py, put this under same directory
import unittest
import test_me

suite = unittest.TestLoader().loadTestsFromModule(test_me)
unittest.TextTestRunner(verbosity=2).run(suite)
运行每个文件,它将显示相同的结果

# python test_me.py - Ran 1 test in 0.000s
# python test_other.py - Ran 1 test in 0.000s

你可以试试下面的方法

# preferred module name would be test_protol as CamelCase convention are used for class name
import TestProtocol

# try to load all testcases from given module, hope your testcases are extending from unittest.TestCase
suite = unittest.TestLoader().loadTestsFromModule(TestProtocol)
# run all tests with verbosity 
unittest.TextTestRunner(verbosity=2).run(suite)
这里有一个完整的例子

文件1:test_me.py

# file 1: test_me.py 
import unittest

class TestMe(unittest.TestCase):
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

if __name__ == '__main__':
    unittest.main()
文件2:test_other.py,将其放在同一目录下

# file 2: test_other.py, put this under same directory
import unittest
import test_me

suite = unittest.TestLoader().loadTestsFromModule(test_me)
unittest.TextTestRunner(verbosity=2).run(suite)
运行每个文件,它将显示相同的结果

# python test_me.py - Ran 1 test in 0.000s
# python test_other.py - Ran 1 test in 0.000s

不幸的是,这也给了我
在0.000秒内运行了0个测试OK
@nwp我已经用一个工作示例更新了我的答案,希望这会有所帮助它现在正在工作。以前它对我不起作用的原因是我在
test\me.py
中有
unittest.main()
(没有
if\uuuu name\uuuu=='\uuu main\uuuu'
),这阻止了测试被找到。@Shaikhul非常好,谢谢分享。您将如何接收状态代码?不幸的是,这也给了我
在0.000s内运行了0个测试OK
@nwp我已经用一个工作示例更新了我的答案,希望这会有所帮助它现在正在工作。以前它对我不起作用的原因是我在
test\me.py
中有
unittest.main()
(没有
if\uuuu name\uuuu=='\uuu main\uuuu'
),这阻止了测试被找到。@Shaikhul非常好,谢谢分享。您将如何接收状态代码?