基本使用
import unittest
class Testcase(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
def tearDown(self):
pass
def test01(self):
print("01")
def test02(self):
print("02")
if __name__ == "__main__":
unittest.main()
断言的使用
unittest的执行顺序:
并不是按照代码顺序,而是按照函数名称顺序;
通过添加执行的顺序
if __name__ == "__main__":
#unittest.main()
suite = unittest.TestSuite()
'''
suite.addTest(TestCase01('test_06'))
suite.addTest(TestCase01('test_04'))
suite.addTest(TestCase01('test_02'))
suite.addTest(TestCase01('test_05'))
suite.addTest(TestCase01('test_01'))
suite.addTest(TestCase01('test_07'))
'''
tests =[TestCase01('test_06'),TestCase01('test_02'),TestCase01('test_03'),TestCase01('test_05'),TestCase01('test_01')]
suite.addTests(tests)
runner = unittest.TextTestRunner()
runner.run(suite)
unittest中套件的运用
有多个测试脚本py文件时,需要一次性执行测试脚本
import unittest
import sys
sys.path.append("dir")添加当前工程目录
from Unittest.test_case01 import Testcase01
from Unittest.test_case01 import Testcase02
testcase01 = unittest.TestLoader().loadTestsFromTestCase(Testcase01)
testcase02 = unittest.TestLoader().loadTestsFromTestCase(Testcase02)
suote = unittest.TestCase([testcase01,testcase02])
unittest.TextTestRunner().run(suote)
unittest 中条件控制跳过case
@unittest.skip("这个case不执行")
def test_07(self):
print("执行case07")
flag = "adfadfadfadfadsfaqeewr"
s = "fads"
self.assertIn(s, flag, msg="不包含")
@unittest.skipIf(4==5,"执行")
def test_01(self):
print("执行case01")
# res = requests.get(url=url,params=data).json()
data1 = {
"user": "11111"
}
self.assertDictEqual(data1, data)
unittest中case的批量管理
import unittest
import sys
import os
sys.path.append("dir")添加当前工程目录
from Unittest.test_case01 import Testcase01
from Unittest.test_case01 import Testcase02
case_path = os.getcwd() + '/Unittestcase'
discover = unittest.defaultTestLoader.discover(case_path)
unittest.TextTestRunner().run(discover)
您可能感兴趣的文章:Python中Unittest框架的具体使用Python Unittest根据不同测试环境跳过用例的方法Python unittest 简单实现参数化的方法python+unittest+requests实现接口自动化的方法Python 单元测试(unittest)的使用小结Python unittest单元测试框架总结python unittest实现api自动化测试Python Unittest自动化单元测试框架详解