当前位置:网站首页>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')边栏推荐
猜你喜欢
随机推荐
Angr(十)——官方文档(Part1)
File upload function
一、unittest框架和pytest框架的区别
【无标题】
Oh my Zsh and TMUX configuration (personal)
mysql 解决不支持中文的问题
Salt FAQs
常用类的小知识
Attention is all you need 论文精读笔记 Transformer
Small knowledge of common classes
DHCP的配置(以华为eNSP为例)
Snake games
一文学会,三款黑客必备的抓包工具教学
多线程——五大状态
存储、计算、分布式存储篇(收集整理适合小白)
Angr (IV) -- angr_ ctf
静态路由的配置(以华为eNSP为例)
部署主从数据库
Use of dictionary tree
oh-my-zsh和tmux配置(个人)









