当前位置:网站首页>Talking about the difference between unittest and pytest
Talking about the difference between unittest and pytest
2022-07-05 09:16:00 【Software testing】
One 、unittest
# MyFunction.py
def add(a, b):
return a + b
1.unittest unit testing
# unit testing .py
"""
1、 Unit test framework : Automatic verification results
python:unittest perhaps pytest、Java:Junit、TestNG
How to write use cases :
Must be test start
Find the case
A parameterized
"""
import unittest
import myFunction
import HTMLTestRunner
import HTMLTestRunnerNew # Test report rich version
import parameterized # A parameterized
class TestAdd(unittest.TestCase):
''' test add Method '''
def testAddNormal1(self):
""" Normal test addition ,by huozi"""
result = myFunction.add(1, 2)
self.assertEqual(3, result, )
def testAddNormal2(self):
""" Normal test addition , with msg Return information """
result = myFunction.add(4, 2)
self.assertEqual(6, result, ' normal case adopt ')
def testAddError1(self):
""" Test failed to use ,by huozi"""
result = myFunction.add(0, 2)
self.assertEqual(4, result)
def testAddError2(self):
""" The test fails with msg Return information to """
result = myFunction.add(1, 2)
self.assertEqual(0, result, ' Normal integer addition , Don't pass ')
@parameterized.parameterized.expand( # Pass parameters to a two-dimensional array
[[1, 2, 3, ' A parameterized 1'],
[-1, 2, 3, ' A parameterized 2'],
[2, 4, 7, ' A parameterized 3']]
)
def testParamAdd(self, a, b, c, desc):
self._testMethodDoc = desc # Use this _testMethodDoc Parameter passing
result = myFunction.add(a, b)
self.assertEqual(c, result, ' The expected result is %s, The actual result is %s' % (c, result))
if __name__ == '__main__':
# How to write it 0: No test report is generated
# unittest.main() # Execute all use cases
# How to write it 1: Run a single test case
testSuite1 = unittest.TestSuite()
testSuite1.addTest(TestAdd('testAddNormal')) # Run a single test case
# testSuite.addTest(TestAdd('testAddError1'))
# testSuite.addTest(TestAdd('testAddError1'))
# How to write it 2: Run the test cases in a class
testSuite2 = unittest.makeSuite(TestAdd) # Run a class ( Such as TestAdd) All the test cases in it
# How to write it 3: Find the test cases in a directory ( Absolute path ), The document must be marked with test start , All the files are :*.py
testSuite3 = unittest.defaultTestLoader.discover('/Users/ray/PycharmProjects/tmz/day9/cases', 'test*.py')
with open('report.html', 'wb') as fw:
# runner = HTMLTestRunner.HTMLTestRunner(stream=fw, title=' Tianma test report ', description=' Tianma test ',verbosity=3)
runner = HTMLTestRunnerNew.HTMLTestRunner(stream=fw, title=' Tianma test report ', description=' Tianma test ', verbosity=3)
runner.run(testSuite2)
Two 、pytest
1.pytest unit testing
class TestClassOne(object):
def test_one(self):
x = "this"
assert 't'in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
class TestClassTwo(object):
def test_one(self):
x = "iphone"
assert 'p'in x
def test_two(self):
x = "apple"
assert hasattr(x, 'check')
3、 ... and 、unittest and pytest The difference between
3 Use case writing rules
unittest Provides test cases、test suites、test fixtures、test runner Related to the class , Make the test clearer 、 convenient 、 controllable . Use unittest Write use cases , The following rules must be observed :
1. The test file must first import unittest
2. The test class must inherit unittest.TestCase
3. The test method must be “test_” start
4. The test class must have unittest.main() Method
pytest yes python Third party testing framework for , Is based on unittest The extension framework of , Than unittest More concise , More efficient . Use pytest Write use cases , The following rules must be observed :
1. The test file name must be “test_” The beginning or "test" ending ( Such as :test_ab.py)
2. The test method must be “test” start .
3. The test class is named with "Test" start .
summary : pytest It can be executed unittest Style test cases , There is no need to modify unittest Any code for the use case , It has good compatibility . pytest Rich plug-ins , such as flask plug-in unit , It can be used for case error rerun ; also xdist plug-in unit , It can be used for parallel execution of devices .
2 Use case pre and post
1 Use case writing rules provide setUp/tearDown, Before each use case runs 、 Run once after it's over .setUpClass and tearDownClass, Before use case execution 、 After the end , Only run once .
# unittset precondition .py
import unittest
class Test(unittest.TestCase): # Inherit unittest Medium TestCase
@classmethod
def setUpClass(cls) -> None: # setUpClass: All use cases will be executed once before execution , Such as : Open file , Linked database
print('setUpClass')
@classmethod
def tearDownClass(cls) -> None: # tearDownClass: All use cases will be executed once , Such as : Delete the data after registration
print('tearDownClass')
@classmethod
def setUp(self) -> None: # setUp: Each use case will be executed before execution setUp, Such as :
print('setUp')
@classmethod
def tearDown(self) -> None: # tearDown: Each use case will be executed first tearDown, Such as :
print('tearDown')
def testB(self): # Use case execution sequence : With test Sort at the beginning of the last letter
print('testB')
def testA(self):
print('testA')
def testZ(self):
print('testZ')
if __name__ == "__main__":
# unittest.main() # No test report is generated
pass
The results are as follows :
Ran 3 tests in 0.003s
Launching unittests with arguments python -m unittest Use case preconditions .Test in /Users/ray/PycharmProjects/day10
OK
setUpClass
tearDownClass
Process finished with exit code 0
setUp
testA
tearDown
setUp
testB
tearDown
setUp
testZ
tearDown
pytest Provides module level 、 Function level 、 Class level 、 Method level setup/teardown, Than unittest Of setUp/tearDown More flexible .
import pytest
# Method in module
def setup_module():
print("setup_module: Whole .py The module executes only once ")
def teardown_module():
print("teardown_module: Whole test_module.py The module executes only once ")
def setup_function():
print("setup_function: Each use case is executed before it starts ")
def teardown_function():
print("teardown_function: At the end of each use case, it will execute ")
# Test cases in the module 1
def test_one():
print(" Executing test module ----test_one")
x = "this"
assert 'h' in x
# Test cases in the module 2
def test_two():
print(" Executing test module ----test_two")
x = "hello"
assert hasattr(x, 'check')
# Test class
class TestCase():
def setup_class(self):
print("setup_class: Before all use cases are executed ")
def teardown_class(self):
print("teardown_class: After all use cases are executed ")
def setup(self):
print("setup: Each use case is executed before it starts ")
def teardown(self):
print("teardown: At the end of each use case, it will execute ")
def test_three(self):
print(" Executing test class ----test_three")
x = "this"
assert 'h' in x
def test_four(self):
print(" Executing test class ----test_four")
x = "hello"
assert hasattr(x, 'check')
if __name__ == "__main__":
pytest.main(["-s", "test_module.py"])
The results are as follows :
collected 4 items
test_module.py setup_module: Whole .py The module executes only once
setup_function: Each use case is executed before it starts
Executing test module ----test_one
.teardown_function: At the end of each use case, it will execute
setup_function: Each use case is executed before it starts
Executing test module ----test_two
Fteardown_function: At the end of each use case, it will execute
setup_class: Before all use cases are executed
setup: Each use case is executed before it starts
Executing test class ----test_three
.teardown: At the end of each use case, it will execute
setup: Each use case is executed before it starts
Executing test class ----test_four
Fteardown: At the end of each use case, it will execute
teardown_class: After all use cases are executed
teardown_module: Whole test_module.py The module executes only once
Method 2 :pytest Of fixture Method
# conftest.py
# -*- coding: utf-8 -*-
import pytest
@pytest.fixture(scope="function")
def login():
print(" Please enter your account and password first , Then log in ")
yield
print(" Exit login ")
# test_1.py
# -*- coding: utf-8 -*-
import pytest
def test_fix1(login):
print("test_fix1 in test_1.py: You need to log in and then perform the operation ")
def test_fix2():
print("test_fix2 in test_1.py: There is no need to log in and then perform the operation ")
def test_fix3(login):
print("test_fix3 in test_1.py: You need to log in and then perform the operation ")
if __name__ == "__main__":
pytest.main(['-s', 'test_1.py'])
# test_2.py
# -*- coding: utf-8 -*-
import pytest
def test_fix3():
print("test_fix3 in test_2.py: There is no need to log in and then perform the operation ")
def test_fix4(login):
print("test_fix4 in test_2.py: You need to log in and then perform the operation ")
if __name__ == "__main__":
pytest.main(['-s', 'test_2.py'])
The results are as follows :
pytest -s test_1.py
collected 3 items
test_1.py Please enter your account and password first , Then log in
test_fix1 in test_1.py: You need to log in and then perform the operation
. Exit login
test_fix2 in test_1.py: There is no need to log in and then perform the operation
. Please enter your account and password first , Then log in
test_fix3 in test_1.py: You need to log in and then perform the operation
. Exit login
3 Assertion
unittest Provides assertEqual、assertIn、assertTrue、assertFalse.
assertEqual: Judge whether the first parameter and the second parameter of the assertion are equal , If not, the test fails
usage : assertIn(key, container, message)
key: A string that checks its existence in a given container
container: String in which to search for key strings
message: As a string statement of the message displayed when the test message fails .
assertIn: Used in unit tests to check whether the string is contained in other strings . This function will use three string parameters as input , And return a Boolean value according to the assertion condition . If key Contained in the container string , It will return true, Otherwise return to false.
usage : assertIn(key, container, message)
Parameters :assertIn() Accept the description of the following three parameters :
key: A string that checks its existence in a given container
container: String in which to search for key strings
message: As a string statement of the message displayed when the test message fails .
assertTrue: Judge whether it is true
assertFalse: Judge whether it is false
pytest Use it directly assert expression .
assert: Used to determine an expression , In the expression, the condition is false Trigger exception when .
4 The report
unittest Use HTMLTestRunnerNew library .
pytest Yes pytest-HTML、allure plug-in unit .
5 Fail to run again
unittest No such function .
pytest Support case execution failure rerun ,pytest-rerunfailures plug-in unit .
6 A parameterized
unittest Depend on ddt Library or parameterized library .
# unit testing .py
import unittest
import myFunction
import HTMLTestRunner
import HTMLTestRunnerNew # Test report rich version
import parameterized # A parameterized
class TestAdd(unittest.TestCase):
''' test add Method '''
@parameterized.parameterized.expand( # Pass parameters to a two-dimensional array
[[1, 2, 3, ' A parameterized 1'],
[-1, 2, 3, ' A parameterized 2'],
[2, 4, 7, ' A parameterized 3']]
)
def testParamAdd(self, a, b, c, desc):
self._testMethodDoc = desc # Use this _testMethodDoc Parameter passing
result = myFunction.add(a, b)
self.assertEqual(c, result, ' The expected result is %s, The actual result is %s' % (c, result))
pytest Use it directly @pytest.mark.parametrize Decorator
@allure.epic("SOS Interface automation testing ")
class TestCaseRunner:
@allure.feature(" Process management / Risk management / Intervention task report ( new )- Inquire about ")
@pytest.mark.parametrize("case", CaseExcuteUtils.get_case_list("soscases/progressManagement/taskreport", case_tag))
def test_task_report(self, case):
"""
Parameterized execution of test cases
:param case:
:return:
"""
print(case.description)
allure.dynamic.title(case.description)
CaseExcuteUtils.excute_case(case, data)
7 Use case classification execution
1.unittest All test cases are executed by default , You can load testsuite Execute some module test cases ;
2.pytest Can pass @pytest.mark To mark test cases , Execute the command with the parameters “-m” You can run the marked use case .
The house needs to be built layer by layer , Knowledge needs to be learned at one point one . We should lay a good foundation in the process of learning , More hands-on practice , Don't talk much , The last dry goods here ! I stayed up late to sort out the stages ( function 、 Interface 、 automation 、 performance 、 Test open ) Skills learning materials + Practical explanation , Very suitable for studying in private , It's much more efficient than self-study , Share with you .
Get off w/x/g/z/h: Software testing tips dao
Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .
边栏推荐
- [technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- Hosting environment API
- Nodemon installation and use
- Codeworks round 638 (Div. 2) cute new problem solution
- AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
- Newton iterative method (solving nonlinear equations)
- Mengxin summary of LIS (longest ascending subsequence) topics
- How many checks does kubedm series-01-preflight have
- Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
猜你喜欢
Applet (global data sharing)
OpenGL - Model Loading
图神经网络+对比学习,下一步去哪?
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Applet customization component
Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
Codeworks round 639 (Div. 2) cute new problem solution
Editor use of VI and VIM
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
随机推荐
Oracle advanced (III) detailed explanation of data dictionary
【阅读笔记】图对比学习 GNN+CL
Codeworks round 638 (Div. 2) cute new problem solution
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
我的一生.
Blue Bridge Cup provincial match simulation question 9 (MST)
Nodemon installation and use
云计算技术热点
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
Confusing basic concepts member variables local variables global variables
MPSoC QSPI flash upgrade method
Hi Fun Summer, play SQL planner with starrocks!
Hosting environment API
My life
Uni app implements global variables
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
fs. Path module
Applet global style configuration window
File server migration scheme of a company