如何在python中的另一个文件中调用class的方法[单元测试]
How to call a method of class in another file in python [unit test]
我想在另一个文件中调用class的方法,class正在使用unittest.Testcase。您可以在下面找到示例代码段,
class Introduction(unittest.TestCase):
def test_case1(self):
print "test 1"
def test_case2(self):
print "test 2"
if__name__=="main"
unittest.main()
这里我可以使用下面的逻辑
调用整个 class
introduction = unittest.TestLoader().loadTestsFromTestCase(Introduction)
unittest.TextTestRunner(verbosity=3).run(introduction)
但我想在另一个文件中调用一个 test_case2 方法,你能帮帮我吗?
提前致谢,
兰吉斯
我得到了从class调用单个方法的逻辑,请找到下面的逻辑来解决
import Introduction
suite = unittest.TestSuite()
suite.addTest(Introduction('test_case1'))
print suite
unittest.TextTestRunner().run(suite)
你可以试试这个:
首先,您应该将包含 Introduction.test_case2 的 python 模块导入到您要从中调用 test_case2 的脚本中(让我们称之为 "main_script" )
import unittest
from test_module_name import Introduction
现在您可以这样做来调用 "main_script"
中的 test_case2
if __name__ == '__main__':
unittest.main(defaultTest="Introduction.test_case2", exit=False)
我想在另一个文件中调用class的方法,class正在使用unittest.Testcase。您可以在下面找到示例代码段,
class Introduction(unittest.TestCase):
def test_case1(self):
print "test 1"
def test_case2(self):
print "test 2"
if__name__=="main"
unittest.main()
这里我可以使用下面的逻辑
调用整个 classintroduction = unittest.TestLoader().loadTestsFromTestCase(Introduction)
unittest.TextTestRunner(verbosity=3).run(introduction)
但我想在另一个文件中调用一个 test_case2 方法,你能帮帮我吗?
提前致谢, 兰吉斯
我得到了从class调用单个方法的逻辑,请找到下面的逻辑来解决
import Introduction
suite = unittest.TestSuite()
suite.addTest(Introduction('test_case1'))
print suite
unittest.TextTestRunner().run(suite)
你可以试试这个:
首先,您应该将包含 Introduction.test_case2 的 python 模块导入到您要从中调用 test_case2 的脚本中(让我们称之为 "main_script" )
import unittest
from test_module_name import Introduction
现在您可以这样做来调用 "main_script"
中的 test_case2if __name__ == '__main__':
unittest.main(defaultTest="Introduction.test_case2", exit=False)