基本使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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的执行顺序:
并不是按照代码顺序,而是按照函数名称顺序;
通过添加执行的顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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文件时,需要一次性执行测试脚本
1
2
3
4
5
6
7
8
9
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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的批量管理
1
2
3
4
5
6
7
8
9
10
11
|
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)
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/LinxiHuang/p/13191088.html