当前位置:网站首页>3、 Five operation modes of unittest test cases
3、 Five operation modes of unittest test cases
2022-07-25 10:21:00 【Christmas gift box】
TestCase usage :
import os
import unittest
class EcshopLogin(unittest.TestCase):
# The test case
def test01_xiaoming(self):
print(" Test Xiao Ming ")
# The test case
def test02_weiwei(self):
print(" Test Pico ")
# The test case
def test10_xiaohongi(self):
print(" Test Xiaohong ")
if __name__ =='__main__':
suite = unittest.TestSuite()
testcase = unittest.defaultTestLoader.discover(start_dir=os.getcwd(),pattern='*.py')
suite.addTests(testcase)
unittest.main(defaultTest='suite')
#unittest.TextTestRunner().run(suite) There is no difference between the results of the two calling methods 1. Command line operation mode :python -m unittest -v ecshop_login.EcshopLogin -k *_weiwei
python -m Run a gate module in a script
python -v --version It means more detailed output
ecshop_login.EcshopLogin Module name . Class name . Method name
-k Matching mode :
1. wildcard :-k *_weiwei
2. character string :-k weiwei
Suitable for integration jenkins When you use , All command line methods are called non GUI The way
postman: Not GUI,newman
jmeter:jmeter The order of
2. Use unittest.main Method , Run as a module
To configure pycharm Environment , Or use python Module name .py Way to run
if __name__ =='__main__':
print('__________')
unittest.main()Read the results of execution :
. success
F Failure self.assertTrue(0)
E error raise Exception(” Custom exception ”)
S skip @unittest.skip(" This use case does not execute ")
Use case execution sequence :
according to ASCLL Code rules :【0-9 A-Z a-z】A=65, a=97
So the use case is named after 01-09, stay 10-99
The bottom principle of the frame :
module='__main__', The road strength of the test case __main__ Represents the current module defaultTest=None, Name of the case to be tested , The default is all example :defaultTest=["EcshopLogin.test01_xiaoming"] argv=None, Accept external parameters testRunner=None, Test runner , Default TextTestRunner- Text format testLoader=loader.defaultTestLoader, Specifies to use the default test case loader , exit=True, Whether to end after the test is completed python Program verbosity=1, similar -v Yes 0,1,2, failfast=None, Whether to terminate the test in case of failure catchbreak=None, Capture replication buffer=None, Whether to capture the output stream warnings=None, *, Whether to display a warning message tb_locals=False
What test suite does it use to run only part of the use cases :
3. A single use case :
if __name__ =='__main__':
suite = unittest.TestSuite()
suite.addTest(EcshopLogin("test01_xiaoming"))
suite.addTest(EcshopLogin("test02_weiwei"))
#unittest.main(defaultTest='suite') There is no difference between the results of the two calling methods
unittest.TextTestRunner().run(suite)4. Use case set :
if __name__ =='__main__':
suite = unittest.TestSuite()
testcase = [EcshopLogin("test01_xiaoming"),EcshopLogin("test10_xiaohongi")]
suite.addTests(testcase)
unittest.main(defaultTest='suite')5. File with .py End all use cases :
#print(os.getcwd()) Get path
if __name__ =='__main__':
suite = unittest.TestSuite()
testcase =unittest.defaultTestLoader.discover(start_dir=os.getcwd(),pattern='*.py')
suite.addTests(testcase)
unittest.main(defaultTest='suite')边栏推荐
猜你喜欢
随机推荐
NPM details
@Import, conditional and @importresource annotations
多线程——五大状态
Fastdfs离线部署(图文)
复现 SSL_Anti-spoofing, 使用 wav2vec 2.0 和数据增强的自动说话人认证的欺骗攻击与深度伪造检测
VS Code 连接远程 Jupyter 服务器
Simple addition calculator
shortest-unsorted-continuous-subarray
多数相合问题总结
Angr (V) - angr_ ctf
链表相关(设计链表及环链表问题)
Angr(九)——angr_ctf
Number theory -- negative Radix conversion
Angr(七)——angr_ctf
UE4 碰撞(Collsion)
JSONObject解析json格式的终极总结
for循环:水仙花案例
JS encryption parameter positioning
一文学会,三款黑客必备的抓包工具教学
修改mysql的分组报错Expression #1 of SELECT list is not in GROUP








