当前位置:网站首页>Unittest test framework
Unittest test framework
2022-06-12 08:59:00 【Ma TA Fei Yan &lin_ li】
Unittest The test framework
1、UintTest yes python Built in unit test framework , Have the ability to write use cases 、 Organize use cases 、 Execute use cases 、 Conditions for automated frameworks such as output reports
2、UintTest The unit testing framework is not only applicable to unit testing , It can also be applied to WEB automated testing 、 Development and implementation of interface automation test . It has a wide range of uses
Four components
testcase testfixture textsuite testrunner
test suite :TestSuite
The common methods are as follows :
⑴unittest.TestSuite()
①addTest(): Add a single test case method
②addTests([…]): Add multiple test case methods , There is a list of method names
⑵unittest.TestLoader()
①loadTestsFromTestCase( Test class name ): Add a test class
②loadTestsFromModule( Module name ): Add a module
③discover( The directory of the test case ): Specify the directory to load , It will automatically find all test cases in this directory that conform to the naming rules
import unittest
class Test(unittest.TestCase):
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
def test_4(self):
print(4)
def test_5(self):
print(5)
def add(self):
print(" This sample will not be executed ")
if __name__ =='__main__':
# Execute all use cases
# unittest.main()
# Create a suite
suit = unittest.TestSuite()
# Add use cases
# suit.addTest(Test("test_3"))
suit.addTests([Test("test_3"),Test("test_2")])
# Execute use cases
run = unittest.TextTestRunner()
run.run(suit)
Test firmware :TestFixture
1、 It is used to build and destroy the test case environment . That is, the environment construction before use case execution , Restore the environment after use case execution . By covering TestCase Of setUP() and tearDown() Method to implement
2、setUP() Method :
⑴ initialization . Execute... Before executing the test , Used to prepare the environment required for this test
⑵ For example, you need to log in before testing token And so on are the environments that test cases need
⑶ For example, you need to access the database in the test case , So it can be setUp() Database connection to initialize
⑷setUP() Method can also be used to define some public variables ( data )、 Code etc. . In this way, these public variables can be called in the entire test class 、 data
3、tearDown() Method :
⑴ Execute... After executing the test , Used to restore the environment before the next test ( Follow setUP In the same way )
⑵tearDown() The process of the method is very important , Leave a clean environment for the next test , So as not to affect the next test
⑶ For example tearDown() Clear the data generated in the database , Then close the connection
4、UintTest There are two ways to use the test firmware in the framework
⑴ One is based on the test method ( Class method ) For the dimension of setUp() and tearDown(): Before and after each test method is executed
⑵ One is to test classes (TestCase) For the dimension of setUpClass() and tearDownClass(): Execute once before and after each test class ( The test method will not be executed before and after )
The first way to use it :
import unittest
class Test(unittest.TestCase):
def setUp(self) -> None:
print(" Open the browser ")
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
def tearDown(self) -> None:
print(" Close the browser ")
if __name__ =='__main__':
# Execute all use cases
unittest.main()

The second way to use it :
import unittest
class Test(unittest.TestCase):
@classmethod # Indicate that this is a class method executed in the dimension of class
def setUpClass(cls):
print("=== Start executing tests ===")
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
@classmethod # Indicate that this is a class method executed in the dimension of class
def tearDownClass(cls):
print("=== End test execution ===")
if __name__ =='__main__':
# Execute all use cases
unittest.main()

Assertion
import unittest
from selenium import webdriver
class Test(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Firefox()
self.driver.get("https://www/baidu.com")
self.title = self.driver.title
print(self.title)
return self.title
def test1(self):
self.assertEqual(self.setUp()," use Baidu Search , You will know ")
if __name__ == '__main__':
unittest.main()
Skip the test
unittest Several decorators are provided to skip the test :
import unittest
class MyTestCase(unittest.TestCase):
# Skip the test , Parameter is the reason why the test was skipped
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
# When the condition is true , Skip the test
@unittest.skipIf(1>3,"if true")
def test_format(self):
print("test_format")
pass
# Unless the condition is true , Otherwise, skip the test
@unittest.skipUnless(1>3, "if not true")
def test_windows_support(self):
print("test_windows_support")
pass
if __name__ == "__main__":
unittest.main()
Subtest
In a test , Pass in different parameters to test the same method ,subTest Subtests can meet this requirement , And the failure of a single sub test does not affect the execution of subsequent sub tests .
import unittest
class NumbersTest(unittest.TestCase):
def test_even(self):
""" Test that numbers between 0 and 5 are all even. """
for i in range(0, 6):
with self.subTest(i==i):
self.assertEqual(i % 2, 0)
if __name__ == "__main__":
unittest.main()

边栏推荐
- Gets the number of occurrences of a character in a string
- node示例后台搭建
- You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
- 利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
- mySql学习记录——二、mySql建表命令
- 《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
- When the uniapp page jumps with complex data parameters.
- ip、DNS、域名、URL、hosts
- Jump to an interface at a specified time. (Web Development)
- Implementing architecture caching in MySQL under redis server environment
猜你喜欢

【字符集九】gbk拷贝到Unicode会乱码?

Does database and table splitting cause reading diffusion problems? How to solve it?

Unittest测试框架

Background attribute compound writing

Set up redis sentinel cluster (instance):
![[computer use] how to change a computer disk into a mobile disk?](/img/ff/843f4220fcaefc00980a6edc29aebf.jpg)
[computer use] how to change a computer disk into a mobile disk?

EIP-1559

The classic dog contract of smart contract (I)

Handling abnormal data

Redis installation test
随机推荐
Knowledge points of 2022 system integration project management engineer examination: project cost management
(十四)InputField逻辑分析
ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘
torch.logical_and()方法
Gets the number of occurrences of a character in a string
[computer use] how to change a computer disk into a mobile disk?
Chapter 7 - more flexible location of memory addresses
Chapter 8 - two basic problems of data processing
[character set 6] wide string and multi byte character conversion
Inheritance of row height
Dynamically create and submit forms
You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
Occupied occupied occupied occupied occupied
Domain name mapping to specified IP
分库分表会带来读扩散问题?怎么解决?
43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
API handling Android security distance
Adjust SVG width and height
Loading circling effect during loading