当前位置:网站首页>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()
边栏推荐
- 软件测试基础知识(思维导图)
- Auditing相关注解
- Why the future of digitalization depends on 3D real-time rendering
- Postman快速上手
- C language libcurl cross compilation
- 为什么数字化未来取决于3D实时渲染
- Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"
- Stm32f105rbt6 internal flash debugging
- Bl602 development environment setup
- Three control methods of TestNG execution sequence
猜你喜欢

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

How to read a Book

IDEA集成SVN代码管理常用功能

Landmark buildings around the world

Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)

Good news! Ruiyun technology was awarded the member unit of 5g integrated application special committee of "sailing on the sea"

排序还需要了解的信息以及链表

更新|3DCAT实时云渲染 v2.1.2版本全新发布

Sorting also needs to know the information and linked list

CVE-2022-33891 Apache spark shell 命令注入漏洞复现
随机推荐
Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number
Tme2022 campus recruitment background development / operation development / business operation and maintenance / application development written examination (I) a little self analysis of programming q
Resttemplate realizes the unified encapsulation (printable log) of post, put, delete, get, set request and file upload (batch files and parameters) through generics
Postman get started quickly
泛域名配置方法
Basic operation of bidirectional linked list
如何选择数字孪生可视化平台
绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
二叉树的相关操作
直击考点:PMP考试中常见敏捷知识点汇总
“Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0”问题解决
Which futures account is the best and safest
绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)
STM32F105RBT6 内部flash调试
Mock service Moco series (I) - introduction, first demo, get request, post request
Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
云流化和云桌面有什么关系
期货开户哪家最好最安全
使用sqldeveloper连接mysql
Why the future of digitalization depends on 3D real-time rendering