当前位置:网站首页>[software testing] - unittest framework for automated testing
[software testing] - unittest framework for automated testing
2022-07-28 02:39:00 【Ombré_ mi】
Catalog
The execution order of use cases
unittest It's a Python Version of Junit,Junit yes Java Unit test framework in ;
In this chapter, we learn the unit testing framework unittest, Unit testing here refers to the smallest software design unit ( modular ) To verify , stay UI Automated testing , Our unit tests are mainly aimed at UI The function of the interface is tested automatically . So here we should pay attention not to contact Java Of Junit The unit test framework is confused ;
unittest Framework analysis
unittest yes python The unit test framework of , It mainly has the following functions :
Provides use case organization and execution : When you have only a few test cases , You don't have to think about the organization of use cases , however , When there are hundreds of test cases , A lot of test cases are stacked together , There are problems such as scalability and maintainability , At this point, we need to consider the specification and organization of use cases . Unit testing framework is to solve this problem .
Provide rich comparison methods : After the use case is executed, the actual results need to be compared with the expected results ( Assertion ), So as to determine whether the use case can pass smoothly . Unit tests generally provide rich assertion methods . for example , Judge equal / It's not equal 、 contain / It doesn't contain 、True/False And so on .
Provide rich logs : When the test case fails to execute, it can throw a clear reason for the failure , When all use cases are executed, rich execution results can be provided . for example , Total execution time , Number of failed cases , Number of successful cases, etc .
unittest There are four important concepts ,test fixture,test case,test suite,test runner.
- Test Fixture
Build and destroy a test case environment , It's just one. fixture, By covering setUp() and tearDown() Method to implement .
setUp() Method can be used to build the test environment , For example, get the driver of the browser to be tested , Or if you need to access the database during the test , So it can be setUp() Initialize by establishing a database connection in .
tearDown() Method to destroy the environment , You can close the browser , Close database connection , Clear the data generated in the database .
- Test Case
One TestCase The example of is a test case . A test case is a complete test process , Including the construction of preparation environment before testing (setUp)、 The code to implement the test process , And the restoration of the environment after testing (tearDown). unit testing (unit test) The essence of is here , A test case is a complete test unit , You can verify a function .
- Test Suite
Verification of a function often requires multiple test cases , Multiple test cases can be assembled and executed together , This produces the test suite TestSuite The concept of .Test Suit Used to assemble multiple test cases ;
- Test Runner
Test execution is also a very important concept , stay unittest In the frame , adopt TextTestRunner Class provides the run() Method to execute test suite/test case.
Here is a use unittest The script for the framework :
from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
class Baidu1(unittest.TestCase):
def setUp(self):
print("-----setUp-----")
self.driver = webdriver.Chrome()
self.url = "https://www.baidu.com/"
self.driver.maximize_window()
time.sleep(3)
def tearDown(self):
print("-----tearDown-----")
self.driver.quit()
def test_hao(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_link_text("hao123").click()
time.sleep(6)
def test_hbaidu(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_id("kw").send_keys(" A sudden vacation ")
driver.find_element_by_id("su").submit()
time.sleep(5)
print(driver.title)
# self.assertNotEqual(driver.title, " use Baidu Search _ Baidu search ", msg=" It's not equal ")
# self.assertTrue("beautiful"=="beauty", msg="Not Equal!")
time.sleep(6)
def saveScreenAsPhoto(self, driver, file_name):
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
time.sleep(3)
if __name__ == "__main__":
unittest.main()(1) Test firmware ( The method of fixing in the frame )
setUp() Method , Equipment work of test environment and data ;tearDown() Method , Clean up after test execution .
(2) The test case
With def test_ The method of naming at the beginning , It's the test method , When running the whole class, it will execute by default , A use case is a method .
(3) test suite
Organize the test cases together to test as a whole , Must be used by inheritance .
unittest Provides a holistic view of main() Method , Using it, you can easily turn a unit test module into a test script that can be run directly .main() Method to search all contained in the module with ”test" Named test method , And automatically execute them .
Batch script execution
Build test suite
When we add the tested functions and corresponding test cases , We need to organize multiple test cases to execute , that
unittest How does the framework extend and organize the new test cases ? It uses the test suite mentioned above Test Suite
Suppose we have written testbaidu1.py,testbaidu2.py Two documents , So how can we execute these two files at the same time ?
testbaidu1.py
from selenium import webdriver
import unittest
import time
import os
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
class Baidu1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.url = "https://www.baidu.com/"
self.driver.maximize_window()
time.sleep(3)
def tearDown(self):
self.driver.quit()
def test_hao(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_link_text("hao123").click()
time.sleep(6)
def test_hbaidu(self):
driver = self.driver
url = self.url
driver.get(url)
driver.find_element_by_id("kw").send_keys(" A sudden vacation ")
driver.find_element_by_id("su").submit()
time.sleep(5)
print(driver.title)
time.sleep(6)
def saveScreenAsPhoto(self, driver, file_name):
if not os.path.exists("./image"):
os.makedirs("./image")
now = time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))
driver.get_screenshot_as_file("./image/" + now + "-" + file_name)
time.sleep(3)
if __name__ == "__main__":
unittest.main()testbaidu2.py
from selenium import webdriver
import unittest
import time
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
class Baidu2 (unittest.TestCase) :
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.driver.maximize_window()
self.verificationErrors=[]
self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
def test_hao(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text(" Journalism ").click()
time.sleep(6)
self.assertTrue("123" == "1234", msg="not true")
time.sleep(3)
def test_baidusearch(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(u" Celebrate more than ")
driver.find_element_by_id("su").click()
time.sleep(6)
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
def is_alert_present(self):
try:
self.driver.switch_to.alert
except NoAlertPresentException as e:
return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to.alert
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
if __name__ == "__main__":
unittest.main(verbosity=2)addTest()
TestSuite Class addTest() Methods can assemble test methods from different test classes into test suites , however addTest() Only one test method in one class can be assembled into the test suite at a time .
The way is as follows : take testbaidu1.py、testbaidu2.py Put the test methods in a test suite , stay testsuite.py To realize .
testsuite.py
import unittest
from src.Test import testbaidu1, testbaidu2
def createsuite():
# addTeat
suite = unittest.TestSuite()
suite.addTest(testbaidu1.Baidu1("test_hao"))
suite.addTest(testbaidu1.Baidu1("test_hbaidu"))
suite.addTest(testbaidu2.Baidu2("test_hao"))
suite.addTest(testbaidu2.Baidu2("test_baidusearch"))
return suite
if __name__ == "__main__":
suite = createsuite()
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)But there are two inconveniences in the above practice , Hinder the rapid execution of scripts , Must be modified every time testsuite.py:
- You need to import all related py file , such as import testbaidu1, Each new script needs to be imported
- addTest Only one test method can be added at a time , If one py In file 10 Test methods , If they are to be assembled into the test suite , It needs to be increased 10 Time
makeSuite() and TestLoader() Application
stay unittest The framework provides makeSuite() Methods ,makeSuite It can realize all the tests in the test case class case Test suite composed of TestSuite ,unittest call makeSuite When , Just pass in the test class name .
TestLoader Test suite for creating classes and modules , In general , send TestLoader().loadTestsFromTestCase(TestClass) To load the test class .
runall.py
import unittest
import testbaidu1
import testbaidu2
# Manually add cases to the suite ,
def createsuite():
suite = unittest.TestSuite()
# Add test cases to the test container ( Kit ) in
suite.addTest(unittest.makeSuite(testbaidu1.Baidu1))
suite.addTest(unittest.makeSuite(testbaidu2.Baidu2))
return suite
'''
suite1 = unittest.TestLoader().loadTestsFromTestCase(testbaidu1.Baidu1)
suite2 = unittest.TestLoader().loadTestsFromTestCase(testbaidu2.Baidu2)
suite = unittest.TestSuite([suite1, suite2])
return suite
'''
if __name__ == "__main__":
suite = createsuite()
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
after makeSuite() and TestLoader() The introduction of , We don't need one py File test class , You only need to import it once . So can you test classes without adding specifications every time ?
discover() Application
discover Is recursively to its subdirectory, starting from the specified directory , Find all test modules and return an object containing them TestSuite , Then load the only test file matching the pattern ,discover The parameters are discover(dir,pattern,top_level_dir=None)
runall1.py
import unittest, csv
import os, sys
import time
# Manually add cases to the suite ,
def createsuite():
discover = unittest.defaultTestLoader.discover('../test', pattern='test*.py', top_level_dir=None)
print(discover)
return discover
if __name__ == "__main__":
suite = createsuite()
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
The execution order of use cases
unittest By default, the order in which the framework loads test cases is based on ASCII The order of the codes , The order of numbers and letters is : 0~9,A~Z,a~z .
therefore , TestAdd Class will take precedence over TestBdd Class is found , test_aaa() Method will take precedence over test_ccc() Be performed . For test directories and test files , unittest The framework also loads test cases according to this rule .
addTest() Methods are executed in increasing order .
Ignore use case execution
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Baidu1(unittest.TestCase):
# test fixture, Initialization environment
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.baidu.com/"
self.verificationErrors = []
self.accept_next_alert = True
@unittest.skip("skipping")
def test_baidusearch(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(u" test ")
driver.find_element_by_id("su").click()
driver.find_element_by_id("su").click()
def test_hao(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_link_text("hao123").click()
self.assertEqual(u"hao123_ Internet starts here ", driver.title)
# Judge element Whether there is , Deleting
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what)
except NoSuchElementException as e:
return False
return True
# Judge alert Whether there is , Deleting
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException as e:
return False
return True
# close alert, Deleting
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally:
self.accept_next_alert = True
# test fixture, Clear the environment
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
# Execute use cases
unittest.main()unittest Assertion
Automated testing , For each individual case Come on , One case In the execution result of , There must be expected results and actual results , To judge the right case Pass or fail , stay unittest The library provides a number of practical ways to check expected and actual values , To verify case Result , Generally speaking , The inspection conditions are roughly divided into equivalence , Logical comparison and other , If the given assertion passes , The test will continue to the next line of code , If the assertion fails , Corresponding case The test will stop immediately or generate an error message ( Generally, you can print error messages ) , But don't affect others case perform .
unittest The unit test library provides a standard xUnit assert methods . Here are some common assertions :

HTML Report generation
After the script is executed , Still need to see HTML The report , Let's go through HTMLTestRunner.py To generate test reports .HTMLTestRunner Support python2.7.python3 You can see http://blog.51cto.com/hzqldjb/1590802 To make changes .
HTMLTestRunner.py file , Download address : http://tungwaiyip.info/software/HTMLTestRunner.html
After downloading, put it in testcase To go or put into a directory ...\Python27\Lib Under the table of contents (windows).
modify runall1.py
import HTMLTestRunner
import os
import sys
import time
import unittest
# Manually add cases to the suite ,
def createsuite():
discover = unittest.defaultTestLoader.discover('../test', pattern='test*.py', top_level_dir=None)
print(discover)
return discover
if __name__ == "__main__":
curpath = sys.path[0]
# Take the current time
now = time.strftime("%Y-%m-%d-%H %M %S", time.localtime(time.time()))
if not os.path.exists(curpath + '/resultreport'):
os.makedirs(curpath + '/resultreport')
filename = curpath + '/resultreport/' + now + 'resultreport.html'
with open(filename, 'wb') as fp:
# Out html The report
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u' Test report ', description=u' Use case execution ', verbosity=2)
suite = createsuite()
runner.run(suite)边栏推荐
- Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
- Interviewer: what is the factory method mode?
- 【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
- LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines
- 0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
- Wechat campus bathroom reservation applet graduation design finished product (3) background function
- ps 简单使用
- Flask1.1.4 werkzeug1.0.1 source code analysis: Blueprint
- MySQL's way to solve deadlock - lock analysis of common SQL statements
- Wechat campus bathroom reservation applet graduation design finished product (2) applet function
猜你喜欢

unordered_map的hash function及hash bucket存储方式探索

When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way

1313_pyserial的安装以及文档的生成

Leetcode hot topic Hot 100 - > 1. Sum of two numbers

Explore flex basis

正则表达式

Wechat campus bathroom reservation applet graduation design finished product (3) background function

【自我成长网站收集】

【软件测试】—— 自动化测试之unittest框架

Manual installation of Dlib Library
随机推荐
How is insert locked in MySQL? (glory Collection Edition)
windbg
[solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任
Should programmers choose outsourcing companies
ps 简单使用
AWS elastic three swordsman
MySQL锁系列之锁算法详解(荣耀典藏版)
Leetcode judge whether palindrome number
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0
Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
Class notes (5) (1) - 593. Binary search
Three core issues of concurrent programming (glory Collection Edition)
Alipay applet authorization / obtaining user information
MySQL blocking monitoring script
支付宝小程序授权/获取用户信息
A 64 bit 8-stage pipelined adder based on FPGA