Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the Application of unittest Framework in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is "detailed explanation of the application of unittest framework in python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the detailed explanation of the application of unittest framework in python.

1. Unittest is a testing framework embedded in Python and does not require special configuration. 2. Write specifications

Need to import import unittest

The test class must inherit unittest.TestCase

The test method begins with test_

Module and class names are not required

TestCase is understood as writing test cases

TestSuite is understood as a collection of test cases

Test case loading as understood by TestLoader

TestRunner executes test cases and outputs reports

The value of import unittestfrom class_api_login_topup.demo import http_requestfrom class_api_login_topup.http_attr import Get_Attr # reflection gets cookies#. This is the Get_Attr class class Get_Attr in the file http_attr: cookies = Noneclass Login_Http (unittest.TestCase): def _ init__ (self, methodName, url, data, method, expected): super (Login_Http) Self). _ init__ (methodName) # Hyperinheritance self.url = url self.data = data self.expected = expected self.method = method def test_api (self): # normal login res = http_request (). Request (self.url, self.data, self.method, getattr (Get_Attr, 'cookies') if res.cookies: setattr (Get_Attr,' cookies') Res.cookies) try: self.assertEqual (self.expected, res.json () ['code']) except AssertionError as e: print ("test_api's, error is {0}", format (e)) raise e print (res.json ()) if _ _ name__ = =' _ main__': unittest.main ()

Execute one:

Import unittestfrom class_demo_login_topup.http_tools import Login_Httpsuite = unittest.TestSuite () loader = unittest.TestLoader () test_data = [{'url':' http://test.lemonban.com/futureloan/mvc/api/member/login', 'data': {' mobilephone': 'xxxx',' pwd': '123456'},' expected': '10001mm,' method': 'get'} {'url':' http://test.lemonban.com/futureloan/mvc/api/member/login', 'data': {' mobilephone': 'xxxx',' pwd': '12345678'}, 'expected':' 20111', 'method':' get'}, {'url':' http://test.lemonban.com/futureloan/mvc/api/member/recharge', 'data': {' mobilephone': 'xxxx',' amount': '1000'},' expected': '10001mm,' method': 'post'}, {' url': 'http://test.lemonban.com/futureloan/mvc/api/member/recharge',' data': {'mobilephone':' xxxx', 'amount':'-100'} 'expected':' 20117 hours, 'method':' post'}] # ergodic data Execution script addTest single execution for item in test_data: suite.addTest (Login_Http ('test_api', item [' url'], item ['data'], item [' method'], item ['expected'])) # execute with open (' http_TestCase.txt', 'winters, encoding='UTF-8') as file: runner = unittest.TextTestRunner (stream=file, verbosity=2) runner.run (suite) # run result {'status': 1 'code':' 10001', 'data': None,' msg': 'login succeeded'} {'status': 0,' code': '20111',' data': None, 'msg':' username or password error'} {'status': 1,' 'code':' 10001', 'data': {' id': 10011655, 'regname':' honeybee, 'pwd':' E10ADC3949BA59ABBE56E057F20F883E' 'mobilephone':' xxxx', 'leaveamount':' 150000.005, 'type':' 1Qing, 'regtime':' 2021-07-14 1414 1454 type': 08.0'}, 'msg':' recharge succeeded'} {'status': 0,' code': '20117', 'data': None,' msg': 'Please enter a positive amount between 0 and 500000'}

Execution 2: put the data of test_data to run in EXCEL.

Import unittestfrom class_demo_login_topup.http_tools import Login_Httpsuite = unittest.TestSuite () loader = unittest.TestLoader () test_data = HttpExcel ('test_api.xlsx',' python'). Real_excel () for item in test_data: suite.addTest (Login_Http ('test_api', item [' url'], eval (item ['data']), item [' method'], str (item ['expected'])) with open (' http_TestCase.txt',') Encoding='UTF-8') as file: runner = unittest.TextTestRunner (stream=file, verbosity=2) runner.run (suite)

Execute third, directly use decorator ddt

Import unittestfrom class_api_login_topup.demo import http_requestfrom class_api_login_topup.http_attr import Get_Attr # reflection values from ddt import ddt, data, unpackfrom class_demo_login_topup.http_excel import HttpExceltest_data = HttpExcel ('test_api.xlsx',' python'). Real_excel () @ ddtclass Login_Http (unittest.TestCase): @ data (* test_data) def test_api (self) Item): # normal login res = http_request (). Request (item ['url'], eval (item [' data']), item ['method'], getattr (Get_Attr,' cookies') if res.cookies: setattr (Get_Attr, 'cookies', res.cookies) try: self.assertEqual (str (item [' expected']) Res.json () ['code']) except AssertionError as e: print ("test_api's, error is {0}", format (e)) raise e print (res.json ())

Execute ddt mode 1

Import unittestfrom class_demo_login_topup.http_tools import Login_Httpfrom class_demo_login_topup.http_excel import HttpExcelsuite = unittest.TestSuite () loader = unittest.TestLoader () from class_demo_login_topup import http_tools_1suite.addTest (loader.loadTestsFromModule (http_tools_1)) # executes the entire file with open ('http_TestCase.txt', 'walled trees, encoding='UTF-8') as file: runner = unittest.TextTestRunner (stream=file, verbosity=2) runner.run (suite)

Execute mode 2 of ddt

Import unittestfrom class_demo_login_topup.http_tools import Login_Http # method without ddt from class_demo_login_topup.http_excel import HttpExcelsuite = unittest.TestSuite () loader = unittest.TestLoader () from class_demo_login_topup.http_tools_1 import * # http_tools_1 file uses the ddt method suite.addTest (loader.loadTestsFromTestCase (Login_Http)) # to execute the Login_Http class under the http_tools_1 file According to the class to execute with open ('http_TestCase.txt', 'walled examples, encoding='UTF-8') as file: runner = unittest.TextTestRunner (stream=file, verbosity=2) runner.run (suite) so far, I believe you have a deeper understanding of the "detailed understanding of unittest framework application in python", you might as well to do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report