当前位置:网站首页>Unittest framework application
Unittest framework application
2022-07-25 18:07:00 【Si Xiaoyou】
1.UnitTest Application rules of
''' Unittest Application rules of : 1. yes Python Default library . No need to install , Just import it directly . 2. When calling, you must inherit UnitTest.TestCase Class to achieve 3.UnitTest Four components Pre post condition :setup and teardown, It is divided into class Level and function level The initialization of test data will not be put into the pre function One class There can only be class Level and Function One set for each level If we consider the scenario of browser caching common among multiple use cases , Use setupclass, Otherwise use setup Set the front The test case : All test cases are based on test Initial function , The sequence of use cases is fixed , be based on ASCII To sort , Re execution . 0-9,A-Z.a-z The order of , It is suggested that when writing use cases , Sort the numbers No addition test Initial function , Are ordinary functions , It needs to be called to take effect . unittest Single use case execution is supported under , But diabolo is not recommended , Because it is easy to report errors because of the use case association logic Assertion : stay unitTest Next , As long as the code does not report an error , It is considered to be passed . adopt self.assert* Words to call 4.UnitTest To start running , It must be main Function unittest.main() start-up 5.UnitTest The printing of the console is a little puzzling , Don't care too much about the output order of the console 6. When designing test cases, we must consider that the relevance between use cases should be as low as possible , Let each use case exist independently as much as possible . '''
import unittest
class UnitDemo02(unittest.TestCase):
# precondition : It can be understood as class At the beginning of implementation , The initialization behavior performed first ( Class level ), Only once
@classmethod
def setUpClass(cls) -> None:
print(' This is a setUpclass02')
cls.a = ' False bamboo '
# Postcondition : It can be understood as every class At the end of execution , The end of the last execution ( Class level ) Only once
@classmethod
def tearDownClass(cls) -> None:
print(' This is a teardownclass02')
# precondition : It can be understood as the initialization behavior of each test case before execution .( Use case level )
def setUp(self) -> None:
print(' This is the precondition ')
# a = ' False bamboo '
# Postcondition : It can be understood as the ending of each use case after execution ( Use case level )
def tearDown(self) -> None:
print(' This is the post condition ')
# The test case 1
def test_01_a(self):
print(' This is a a')
# print(self.a)
# modify cls.a Value : Class name
# Class name . Attribute changes , You can modify the value of the element , Effective for global
# UnitDemo.a = ' Zhong Ling '
# self. Attribute changes , It can be modified in the function , Invalid for global
# self.a = ' Zhong Ling '
# print(self.a)
# The test case 2
def test_02_A(self):
# print(' This is a A')
# print(self.a)
pass
# The test case 3
def test_03_1(self):
print(' This is a 1')
# This is the second class object
class UnitDemo(unittest.TestCase):
# precondition : It can be understood as class At the beginning of implementation , The initialization behavior performed first ( Class level ), Only once
@classmethod
def setUpClass(cls) -> None:
print(' This is a setUpclass')
cls.a = ' False bamboo 02'
# Postcondition : It can be understood as every class At the end of execution , The end of the last execution ( Class level ) Only once
@classmethod
def tearDownClass(cls) -> None:
print(' This is a teardownclass')
def test_01(self):
# print(' This is a unitDemo Test cases for ')
# print(self.a)
a = 3.1415926123456789
b = 3.141592612345678
self.assertAlmostEqual(a,b,msg = ' incorrect ')
# self.assertEqual(a,b,msg=' incorrect ')
def test_02(self):
# c = self.func(1,2)
# print(c)
c = 10
print(' This is a 02')
UnitDemo.a = c
return c
def test_03(self):
# print(self.test_02())
print(self.a)
# If not test start , Normal function
def func(self,a,b):
print(' This is a func function ')
return a + b
# unittest start-up
if __name__ == '__main__':
unittest.main()
2.UnitTest Automatic test implementation under
''' Unittest Automatic test implementation under A complete process , Different small processes are divided into different use cases to manage . This is easy to maintain '''
import unittest
from class24.web_keys import Keys
class UnitAuto(unittest.TestCase):
# Back and forth
@classmethod
def setUpClass(cls) -> None:
cls.key = Keys('Chrome')
# def setUp(self) -> None:
# self.key = Keys('Chrome')
#
# def tearDown(self) -> None:
# self.key.quit()
@classmethod
def tearDownClass(cls) -> None:
cls.key.quit()
def test_01_login(self):
# key = Keys('Chrome')
self.key.open('http://39.98.138.157/shopxo/index.php?s=/index/user/logininfo.html')
self.key.input('name','accounts','sixiaoyou')
self.key.input('name','pwd','xxx')
self.key.click('xpath','//button[text()=" Sign in "]')
# self.assertEqual() Be similar to assert a == b,msg
# self.assertTrue(self.key.assert_text('link text',' sign out ',' sign out 1'),msg = ' Assertion failed ')
self.assertEqual(' sign out 1',self.key.get_text('link text',' sign out '),msg = ' Assertion failed ')
self.key.sleep(5)
# key.quit()
# def test_02_search(self):
# key = Keys('Chrome')
# self.key.open('http://39.98.138.157/shopxo/index.php')
# self.key.input('id','search-input',' mobile phone ')
# self.key.click('id','ai-topsearch')
# self.key.sleep(5)
# key.quit()
if __name__ == '__main__':
unittest.main()
边栏推荐
- “Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0”问题解决
- What is the relationship between cloud fluidization and cloud desktop
- MySQL lost the previous 0 after the decimal number type select
- MySQL optimistic lock
- What is an IP SSL certificate and how to apply for it?
- Construction of Huffman tree
- 十九岁的总结
- MySQL数据库常用命令
- PageHelper还能结合Lambda表达式实现简洁的分页封装
- "Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
猜你喜欢

LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树

Landmark buildings around the world

PageHelper还能结合Lambda表达式实现简洁的分页封装

为什么数字化未来取决于3D实时渲染

实时云渲染有哪些优势

ORB_SLAM3复现——上篇
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

Wu Enda's machine learning programming operation cannot be suspended pause problem solved

Imx6 rtl8189ftv migration

Could not stop Cortex-M device! Please check the JTAG cable solution
随机推荐
itextpdf实现多PDF文件合并为一个PDF文档
Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report
Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明
Mongodb cluster and sharding
大话DevOps监控,团队如何选择监控工具?
How many points did NPDP pass? How to pass with high scores?
图的相关操作
Unity 贝塞尔曲线的创建
Landmark buildings around the world
SVN客户端(TortoiseSVN)安装及使用说明
Error when starting MySQL on Linux
虚拟偶像代言产品出问题谁负责?
OV7725 yuv 640*[email protected] 配置文件
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
[untitled]
Redis source code and design analysis -- 16. AOF persistence mechanism
NPDP多少分通过?如何高分通过?
BiSeNet v1
MATLAB中join函数使用
Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法