gpt4 book ai didi

python:在单元测试中测试特定功能?

转载 作者:行者123 更新时间:2023-11-28 18:19:49 24 4
gpt4 key购买 nike

我已经知道如何在 python 的 unittest 框架中测试特定的类。但是,我无法测试特定功能。这是我用来选择要测试的类的方法:

if __name__ == '__main__':
test_classes_to_run = [Class1, Class2, Class3]

loader = unittest.TestLoader()

suites_list = []
for test_class in test_classes_to_run:
suite = loader.loadTestsFromTestCase(test_class)
suites_list.append(suite)

big_suite = unittest.TestSuite(suites_list)

runner = unittest.TextTestRunner()
results = runner.run(big_suite)

所以我尝试选择一个特定的功能来做:

if __name__ == '__main__':
testing_class = Class1
test_classes_to_run = [Class1.function1]

loader = unittest.TestLoader()

suites_list = []
for test_class in test_classes_to_run:
suite = loader.loadTestsFromTestCase(test_class)
suites_list.append(suite)

big_suite = unittest.TestSuite(suites_list)

runner = unittest.TextTestRunner()
results = runner.run(big_suite)

但我收到以下错误:

TypeError: issubclass() arg 1 must be a class

最佳答案

就用

unittest.TestLoader.loadTestsFromName('Path.to.Class1.test_function1')

来自documentation of the unittest module :

For example, if you have a module SampleTests containing a TestCase-derived class SampleTestCase with three test methods (test_one(), test_two(), and test_three()), the specifier 'SampleTests.SampleTestCase' would cause this method to return a suite which will run all three test methods. Using the specifier 'SampleTests.SampleTestCase.test_two' would cause it to return a test suite which will run only the test_two() test method. The specifier can refer to modules and packages which have not been imported; they will be imported as a side-effect.

关于python:在单元测试中测试特定功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45925367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com