当前位置:网站首页>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()

边栏推荐
- [GUI development] browsing function implementation model of image processing software
- Dynamic segment tree leetcode six hundred and ninety-nine
- 解压缩zip文件的工具类
- 【sklearn学习】LightGBM
- 数据库不知道哪里出问题
- Dynamically create and submit forms
- 2022.6.11-----leetcode.926
- Introduction to Chang'an chain node certificate, role and authority management
- Construction of memcached cache service under Linux:
- Wechat applet image saving function
猜你喜欢

Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression

Building a cluster: and replacing with error

torch.logical_and()方法

Set up redis sentinel cluster (instance):
![(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit](/img/c1/d56ec09663857afa52f20848aeadac.png)
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit

Close asymmetric key

Flink passes in custom parameters or profiles
![[GUI development] browsing function implementation model of image processing software](/img/37/2162a6047682b9cfc9b8b7c2488068.jpg)
[GUI development] browsing function implementation model of image processing software

2022 safety officer-c certificate special operation certificate examination question bank and simulation examination

Loading font component loading effect
随机推荐
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function
Make a simple page with the websql database of HTML5:
Regular verification user name
Notes used by mqtt (combined with source code)
Domain name mapping to specified IP
第四章-第一个程序
(十二)交互组件Selectable
Implementing architecture caching in MySQL under redis server environment
Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps
Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
第三章 寄存器 (内存访问)
Chapter V -[bx] and loop instructions
[character set 8] char8_ t、char16_ t、char32_ t、wchar、char
[character set 7] what are the wide character codes and multi byte codes of Chinese characters
Get last month, current time and next month
利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
数据库不知道哪里出问题
[character set 6] wide string and multi byte character conversion
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an