当前位置:网站首页>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()
边栏推荐
- Who is responsible for the problems of virtual idol endorsement products?
- Why the future of digitalization depends on 3D real-time rendering
- The new version of 3dcat v2.1.3 has been released. You can't miss these three function updates!
- 2022/7/23
- 如何选择数字孪生可视化平台
- C language libcurl cross compilation
- Lwip之内存与包缓冲管理
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- 十九岁的总结
- TESTNG中的并发测试invocationCount, threadPoolSize, timeOut的使用
猜你喜欢

Installation and operation instructions of SVN client (TortoiseSVN)

Drawing PDF form (II) drawing excel form style in PDF through iText, setting Chinese font, watermark, logo, header and page number

「行话」| 用DevOps高效交付游戏,是种什么体验?

十九岁的总结

Cloud VR: the next step of virtual reality specialization

CH582 BLE 5.0 使用 LE Coded 广播和连接

2022/7/23

云VR:虚拟现实专业化的下一步

"Digital security" alert NFT's seven Scams

Hcip first day experiment
随机推荐
How to read a Book
nodejs 简单例子程序之express
Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
List转换问题
STM8S003F3 uart的使用
Why the future of digitalization depends on 3D real-time rendering
UFT(QTP)-总结点与自动化测试框架
Which futures account is the best and safest
排序还需要了解的信息以及链表
What scenarios have rust, which is becoming more and more mature, applied?
3DCAT v2.1.3新版本发布,这三大功能更新你不容错过!
文件基础知识
SVN客户端(TortoiseSVN)安装及使用说明
UFT (QTP) - summary points and automated test framework
简述Synchronized以及锁升级
Principle and implementation of UDP penetration NAT in P2P
Express of nodejs simple example program
What are the advantages of real-time cloud rendering
Resttemplate realizes the unified encapsulation (printable log) of post, put, delete, get, set request and file upload (batch files and parameters) through generics
十九岁的总结