Rapid construction and operation of UnitTest framework

Keywords: Python

Let 's start with a brief introduction to the unittest framework

1. Structured folder:

Note that the above folders are all packages, that is, when you create a new folder in new, you should not select directory, but select package;

The first step is finished after the folder is built!

2. The second step is the idea of object-oriented

Object oriented is very simple. Object oriented is to treat all functions as separate modules. The lower the coupling (i.e. Association) between modules, the better your structure will be. When you need these functions, you only need to call the corresponding modules. The advantage of this is that if there is an error, it must be an error when you call, instead of writing a lot of code, which makes it impossible Easy to locate the root cause of the error, of course, provided that the module you encapsulate is error free.

3. The third step is to package the module

3.1 encapsulation constant

Put together the same things you use, which will be very convenient to change later, such as the sender, recipient, password, account number, etc

# coding: utf-8

# Mail configuration
mail_host = 'smtp.163.com'
Smtp_Sender = 'warrior_meng08@163.com'
Smtp_Password = 'Authorization code' Smtp_Receivers = ['312652826@qq.com','721@qq.com'] 

Put the above content under the baseInfo package__ init __.py file

3.2 package the method of sending email
Next, encapsulate a method of sending mail

# coding: utf-8

import sys

reload(sys)
sys.setdefaultencoding('utf8')
import baseInfo
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def sendMail(file): f = open(file, 'rb') # Read test report body mail_body = f.read() f.close() # Create an instance with attachments msg = MIMEMultipart() msg['From'] = baseInfo.Smtp_Sender receiver = ','.join(baseInfo.Smtp_Receivers) msg['To'] = receiver msg['Subject'] = 'Python test' # Message body msg.attach(MIMEText('sending email test', 'plain', 'utf-8')) # Construct attachment 1 att1 = mime text (mail_ body, 'base64', 'utf-8') att1['Content-Type'] = 'application/octet-stream' att1['Content-Disposition'] = 'attachment; filename= %s' % file msg.attach(att1) try: smtpObj = smtplib.SMTP() smtpObj.connect(baseInfo.mail_host, 25) smtpObj.login(baseInfo.Smtp_Sender, baseInfo.Smtp_Password) smtpObj.sendmail(baseInfo.Smtp_Sender, receiver, msg.as_string()) print 'Success' except smtplib.SMTPException: print 'Error' 

Put the above code in common - > module - > email_ module.py File, as a module (object), to be called directly in the future.

4. Use cases

Next, let's write the use case first. In order to be representative, let's write the simplest use case first:

# coding: utf-8

import unittest

class Testcases(unittest.TestCase): def setUp(self): print "setUp" def tearDown(self): print "tearDown" def test01(self): print "test01" def test03(self): print "test03" def test02(self): print "test02" 

Is it very simple ~ ~ put the above code under the testcase package Testcases.py In file

5. Run the use case and send the test report

Here we only write three use cases. In actual work, we need to write hundreds of use cases. We can't run them one by one, so the mouse will break So we need to write a method to run all use cases at once. This is also the convenience of unittest framework. The code is as follows:

# coding: utf-8

import unittest
from common.module import email_module

def all_case(): #Your file path case_dir = r"E:\a\mytest\testcase" discover = unittest.defaultTestLoader.discover(case_dir, pattern="test*.py", top_level_dir=None) return discover if __name__=='__main__': #Import HTMLTestRunner modular import HTMLTestRunner #The end must be written.html Oh report_path = r"E:\Your path\report\report.html" fp = open(report_path, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"Test report", description=u"Use case execution") runner.run(all_case()) fp.close() #Call the encapsulated sendmail method with the parameter mail = email_module.sendMail(report_path) print "Email sending Success" 

Put the above code in the runAllTests.py In the file, notice the HTMLTestRunner.py

You can download it here and save the. py file directly

http://tungwaiyip.info/software/HTMLTestRunner.html
If for software testing, interface testing, automation testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.
And then I put this HTMLTestRunner.py Put the file in the lib folder of your Python installation directory

It's all done here! Last run runAllTests.py The file is good once. Don't worry about the mouse point breaking. The following display will appear in your run display box:

 

Note that it will be a little slower to print Success later, because it is calling the method of sending mail, which needs to go through the process of login and sending. Then your inbox will receive the corresponding email:


Download the following attachment, and you can see the process HTMLTestRunner.py The processed test report will not be shown here.
The above content hopes to be helpful to you. Friends who have been helped are welcome to like and comment.

Posted by Kurtismonger on Tue, 19 May 2020 22:06:59 -0700