'''testmath.py'''
import unittest
import ddt
def jiafa(a,b):
if type(a) is str or type(b) is str:
return str(a) + str(b)
return a+b
def chengfa(a,b):
if type(a) is str or type(b) is str:
return 0
return a*b
testdata = [{'a':1,'b':1,'jiafa':2,'chengfa':1},
{'a':'a','b':1,'jiafa':'a1','chengfa':'a'},
{'a':'a','b':'b','jiafa':'ab','chengfa':'ab'}]
@ddt.ddt
class TestMath(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
@ddt.data(*testdata)
def test_jiafa(self,data):
'''测试加法程序'''
try:
self.assertEqual(jiafa(data['a'],data['b']),data['jiafa'])
print(str(data['a']) + '+' + str(data['b']) + '=' + str(data['jiafa']))
except Exception as e:
print(str(data['a']) + '+' + str(data['b']) + '=' + str(data['jiafa'] + ' X'))
raise e
@ddt.data(*testdata)
def test_chengfa(self,data):
'''测试乘法程序'''
try:
self.assertEqual(chengfa(data['a'],data['b']),data['chengfa'])
print(str(data['a']) + '*' + str(data['b']) + '=' + str(data['chengfa']))
except Exception as e:
print(str(data['a']) + '*' + str(data['b']) + '=' + str(data['chengfa'] + ' X'))
raise e
if __name__ == '__main__':
unittest.main()
loadTestsFromTestCase
(testCaseClass)
返回一个套件中包含的所有测试用例,将为每个由test开头的方法创建一个测试用例实例。
import unittest
import testmath
testsuite = unittest.TestLoader().loadTestsFromTestCase(testmath.TestMath)
print(testsuite)
发现返回的对象为<unittest.suite.TestSuite tests=[<testmath.TestMath testMethod=test_chengfa_1>, <testmath.TestMath testMethod=test_chengfa_2>, <testmath.TestMath testMethod=test_chengfa_3>, <testmath.TestMath testMethod=test_jiafa_1>, <testmath.TestMath testMethod=test_jiafa_2>, <testmath.TestMath testMethod=test_jiafa_3>]>
loadTestsFromModule
(module,pattern = None)
返回给定模块中包含的所有测试用例的套件。
testsuite = unittest.TestLoader().loadTestsFromModule(testmath)
print(testsuite)
发现返回的对象为<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<testmath.TestMath testMethod=test_chengfa_1>, <testmath.TestMath testMethod=test_chengfa_2>, <testmath.TestMath testMethod=test_chengfa_3>, <testmath.TestMath testMethod=test_jiafa_1>, <testmath.TestMath testMethod=test_jiafa_2>, <testmath.TestMath testMethod=test_jiafa_3>]>]>
loadTestsFromName(name,module = None)
按照“模块名.类名.方法名”输入字符串,返回测试用例的套件。
例如'testmath'
将使该方法返回3个套件。
例如'testmath.TestAdd'和"testmath.TestAdd.test_add"
将使该方法返回一个套件,该套件将运行test_add。
loader = unittest.TestLoader()
testsuite = loader.loadTestsFromName('testmath')
print(testsuite)
print(' ')
testsuite = loader.loadTestsFromName('testmath.TestMath')
print(testsuite)
print(' ')
testsuite = loader.loadTestsFromName('testmath.TestMath.test_jiafa')
print(testsuite)
print(' ')
发现返回的对象为
<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<testmath.TestMath testMethod=test_chengfa_1>, <testmath.TestMath testMethod=test_chengfa_2>, <testmath.TestMath testMethod=test_chengfa_3>, <testmath.TestMath testMethod=test_jiafa_1>, <testmath.TestMath testMethod=test_jiafa_2>, <testmath.TestMath testMethod=test_jiafa_3>]>]>
<unittest.suite.TestSuite tests=[<testmath.TestMath testMethod=test_chengfa_1>, <testmath.TestMath testMethod=test_chengfa_2>, <testmath.TestMath testMethod=test_chengfa_3>, <testmath.TestMath testMethod=test_jiafa_1>, <testmath.TestMath testMethod=test_jiafa_2>, <testmath.TestMath testMethod=test_jiafa_3>]>
<unittest.suite.TestSuite tests=[<unittest.loader._FailedTest testMethod=test_jiafa>]>
getTestCaseNames
(testCaseClass)
返回测试类的所有方法名的排序序列,即测试顺序
loader = unittest.TestLoader()
name = loader.getTestCaseNames(testmath.TestMath)
print(name)
返回所有的.TestAdd方法名['test_chengfa_1', 'test_chengfa_2', 'test_chengfa_3', 'test_jiafa_1', 'test_jiafa_2', 'test_jiafa_3']
discover(s
tart_dir,pattern ='test * .py',top_level_dir = None )
同之前介绍的 命令行参数的discover作用相同,通过从指定的起始目录递归到子目录中,找到所有测试模块,然后返回包含它们的TestSuite对象。
参数testMethodPrefix
字符串,修改默认查找的测试匹配开头的字符串。默认值为'test'
。影响getTestCaseNames()
所有loadTestsFrom*()
方法
loader = unittest.TestLoader() loader.testMethodPrefix = 'test_j' name = loader.loadTestsFromModule(testmath) print(name)
返回的对象为<unittest.suite.TestSuite tests=[<unittest.suite.TestSuite tests=[<testmath.TestMath testMethod=test_jiafa_1>, <testmath.TestMath testMethod=test_jiafa_2>, <testmath.TestMath testMethod=test_jiafa_3>]>]>
参数sortTestMethodsUsing
修改对用例的排序,默认为按照字母正序排序
比如说我希望按照字母倒序排序
loader = unittest.TestLoader()
loader.sortTestMethodsUsing = lambda x, y:(x < y)-(x > y)
name = loader.loadTestsFromTestCase(testmath.TestMath)
print(name)
这样就是从test_jiafa在test_chengfa之前运行