unittest框架给我们准备了多种执行测试用例的方法。这里我们来学习一些经常用到的操作。
脚本自测
unittest.main会自动收集当前文件中所有的测试用例来执行:
import unittestblog_name = 'hy592070616'
class MyTestClass(unittest.TestCase):
def setUpClass(cls):
print('setUpClass')
def setUp(self):
self.blog_name = 'hy592070616'
print('setUp')
def test_first(self):
blog_name_ref = blog_name + '--machine learning'
self.assertEqual(blog_name_ref, 'hy592070616--machine learning')
def tearDown(self):
print('tearDown')
def tearDownClass(cls):
print('tearDownClass')
if __name__ == '__main__':
unittest.main()
- if __name__ == '__main__':的name和main前后都有两个下划线。我们可以在PyCharm中直接输入main来快速输入这行代码。
- unittest.main(),main后面需要加上括号,否则无法正常执行。
- unittest.main(),是对上方脚本进行自测,不影响其他文件的调用。
通过class构造测试集合
我们可以通过unittest.TestLoader().loadTestsFromTestCase加载某个class下面的所有用例。这个是我们的一个测试用例文件testclass.py,包含了4个测试用例和2个class:
# testclass.pyimport unittest
class MyTestClassOne(unittest.TestCase):
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_first(self):
print('test_first')
self.assertEqual(0, 0)
def test_second(self):
print('test_second')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
def tearDownClass(cls):
print('tearDownClass')
class MyTestClassTwo(unittest.TestCase):
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_third(self):
print('test_third')
self.assertEqual(0, 0)
def test_fourth(self):
print('test_fourth')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
def tearDownClass(cls):
print('tearDownClass')
if __name__ == '__main__':
unittest.main()
后续我们在其他的Python文件中导入testclass.py:
import unittestfrom learning_unittest import testclass
if __name__ == '__main__':
testcase = unittest.TestLoader().loadTestsFromTestCase(testclass.MyTestClassOne)
suite = unittest.TestSuite([testcase])
unittest.TextTestRunner().run(suite)
运行结果如下:
Ran 2 tests in 0.000sOK
setUpClass
setUp
test_first
tearDown
setUp
test_second
tearDown
tearDownClass
Process finished with exit code 0
上方代码先将testclass文件中的内容通过import导入,然后使用unittest提供的unittest.TestLoader().loadTestsFromTestCase方法传入testclass文件中的class,即MyTestClassOne。接下来通过unittest.TestSuite([testcase])组装测试用例集合。最后通过unittest.TextTestRunner提供的run方法来执行组装好的集合。但如果:
# testclass.pyimport unittest
class MyTestClassOne(unittest.TestCase):
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_first(self):
print('test_first')
self.assertEqual(0, 0)
def test_second(self):
print('test_second')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
def tearDownClass(cls):
print('tearDownClass')
class MyTestClassTwo(unittest.TestCase):
def setUpClass(cls):
print('setUpClass')
def setUp(self):
print('setUp')
def test_third(self):
print('test_third')
self.assertEqual(0, 0)
def test_fourth(self):
print('test_fourth')
self.assertEqual(0, 0)
def tearDown(self):
print('tearDown')
def tearDownClass(cls):
print('tearDownClass')
if __name__ == '__main__':
testcase = unittest.TestLoader().loadTestsFromTestCase(MyTestClassOne)
suite = unittest.TestSuite([testcase])
unittest.TextTestRunner().run(suite)
即将组装testcase语句写入testclass文件中,则运行testclass文件会执行testclass文件中的所有class,而不是只执行MyTestClassOne:
============================= test session starts =============================platform win32 -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: D:\LearningPython\learning_unittest, inifile:
collected 4 items
testclass.py setUpClass
.setUp
test_first
tearDown
.setUp
test_second
tearDown
tearDownClass
setUpClass
.setUp
test_fourth
tearDown
.setUp
test_third
tearDown
tearDownClass
========================== 4 passed in 0.03 seconds ===========================
Process finished with exit code 0
通过addTest构建测试集合
我们可以通过addTest将某个class下面的测试用例添加到集合,然后执行测试集合:
import unittestfrom learning_unittest import testclass
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(testclass.MyTestClassOne('test_first'))
suite.addTest(testclass.MyTestClassTwo('test_third'))
unittest.TextTestRunner().run(suite)
上述代码从MyTestClassOne类中取了test_first测试用例,从MyTestClassTwo类中取了test_third测试用例,两者组成了一个测试用例集合,运行结果如下:
Ran 2 tests in 0.000sOK
setUpClass
setUp
test_first
tearDown
tearDownClass
setUpClass
setUp
test_third
tearDown
tearDownClass
Process finished with exit code 0
通过discover构建测试集合
我们还可以通过unittest.TestLoader().discover('.')在指定目录中寻找符合条件的测试用例,从而组成测试集合。关于discover的用法请参考代码:
import unittestfrom learning_unittest import testclass
if __name__ == '__main__':
suite = unittest.TestLoader().discover('.')
unittest.TextTestRunner(verbosity=2).run(suite)
这里的discover,我们传递的目录是一个‘.’,代表文件所在目录。执行该文件的话,就会从该文件所在目录中去寻找所有符合条件的测试用例。discover的用法我们会在后续的文章再次讲解。