问题:
- 我们知道根据排序规则,unittest执行测试用例,默认是根据ASCII码的顺序加载测试用例,数字与字母的顺序为:0-9,A-Z,a-z。我们怎么来控制用例执行的顺序呢?
- 一个测试文件,我们直接执行该文件即可,但如果有多个测试文件,怎么进行组织,总不能一个个文件执行吧?
查了一些网上的资料,主要介绍了两种方式:
方式1,通过TestSuite类的addTest方法,按顺序加载测试用例:
class TestStringMethods(unittest.TestCase):
@classmethod
def setUpClass(cls):
num = 0
print(num)
@classmethod
def tearDownClass(cls) -> None:
print('end')
def setUp(self) -> None:
print('hello')
def tearDown(self) -> None:
print('Goodbai')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO', msg="预期结果与实际结果不相等")
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
suite = unittest.TestSuite()
# 一 测试用例模块名直接传入,按照默认排序规则执行用例
# suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestStringMethods))
# 二 根据逐个添加的顺序执行
# suite.addTest(TestStringMethods("test_split"))
# suite.addTest(TestStringMethods("test_upper"))
# 三 根据列表的索引顺序执行,我们使用这种方式:便捷
suite.addTests([TestStringMethods("test_split"), TestStringMethods("test_upper"), TestStringMethods("test_isupper")])
with open('UnittestTextReport02.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)
方式2,通过修改函数名的方式:
import unittest
class TestStringMethods(unittest.TestCase):
def setUp(self) -> None:
print('hello')
def tearDown(self) -> None:
print('Goodbai')
def test_1_upper(self):
self.assertEqual('foo'.upper(), 'FOO', msg="预期结果与实际结果不相等")
def test_2_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_3_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
suite = unittest.TestSuite()
# 测试用例模块名直接传入
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestStringMethods))
with open('UnittestTextReport02.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)
下面看一下第二个问题: 假如测试用例分别放在两个文件中(cmslogin.py,smelogin.py),现在我需要把这两个文件中的用例添加到一个测试套件中来执行,为此我们要重新建立一个叫run_all.py的文件
import unittest
from cmslogin import CmsLoginTest
from smelogin import SmeLoginTest
if __name__ == "__main__":
# 构造测试套件
suite = unittest.TestSuite()
test_cases = [CmsLoginTest("test_login1"),CmsLoginTest("test_login2"),CmsLoginTest("test_login4"),
CmsLoginTest("test_login3"),SmeLoginTest("test_login1"),SmeLoginTest("test_login2")]
suite.addTests(test_cases)
# 执行测试
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
# 还可以用addTests + TestLoader方法来添加用例,但是这种方法是无法对case进行排序的
import unittest
from cmslogin import CmsLoginTest
from smelogin import SmeLoginTest
if __name__ == "__main__":
# 构造测试套件
suite = unittest.TestSuite()
# 第一种方法:传入'模块名.TestCase名'
suite.addTests(unittest.TestLoader().loadTestsFromName('cmslogin.CmsLoginTest'))
suite.addTests(unittest.TestLoader().loadTestsFromName('smelogin.SmeLoginTest'))
# 这里还可以把'模块名.TestCase名'放到一个列表中
suite.addTests(unittest.TestLoader().loadTestsFromNames(['cmslogin.CmsLoginTest','smelogin.SmeLoginTest']))
# 执行测试
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)