This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

series

  • Postman interface use cases are transformed into Python automated test cases
  • Postman Interface Use Cases to Python Automated Test Cases (Part 2)
  • Postman Interface Case to Python Automated Test Case (3)
  • Postman Interface Use Cases to Python Automated Test Cases (4)

In several articles, interface testing is explained in general, but the previous code is not separated from the design, the whole catalog is in one piece, it looks messy and annoying. Sometimes it even seems soulless. A lot of people look at your code, they don’t know how to look at your code, how to make sense of your code.

In my opinion, it is necessary to optimize the design of the code refactoring, let’s see how to split, in fact, it is no more than some of the code package out of the. But how do you break it down. Today I’m going to look at my idea of breaking it down. It’s not really a design, it’s just to show you how I break it down.

So far the code looks like this, and I split it up like this.

The general idea is as follows:

  • 1. In the config directory, save the configuration file

  • 2.common directory for common files

  • 3. Report directory, put test reports

  • 4. Case directory for storing test cases

  • 5. Run file, put the execution code

  • 6. Data directory, which stores case data files

So I made some adjustments to the code along those lines.

The optimized directory looks like this. Let’s take a look at what is implemented in the optimized new run file.


import unittest
from common.HTMLTestRunnerCN import HTMLTestReportCN
if __name__ == "__main__":
    import os
    suit = unittest.TestSuite()
    loader = unittest.TestLoader()
    suit.addTests(loader.discover(os.getcwd()+"/case"))
    path=os.getcwd()+"/report"
    filePath = path+'/report.html'
    fp = open(filePath, 'wb')
    runner = HTMLTestReportCN(
        stream=fp,
        title='Test Report',
        description='Test Report'
    )
    runner.run(suit)
    fp.close()
Copy the code

This is the new run.py file, of course, but other files have also been adjusted. The file that gets the test case has been optimized, and the optimized code looks like this:


import os
def get() :
    reslut = []
    path=os.getcwd()
    f=open(path+"/data/case.txt"."r")
    all=f.readlines()
    for item in all:
        dictone={}
        reslut_all=item.split("|")
        dictone["url"]=reslut_all[0]
        dictone['data']=reslut_all[1]
        dictone['headers']=reslut_all[2]
        dictone['assert']=reslut_all[3]
        dictone['method']=reslut_all[4]
        reslut.append(dictone)
    return reslut
Copy the code

Case, common, and config are all Python packages from which py files can be referenced. Others can create files directly.

At this point our code is layered and detached and adjusted. The whole extraction process is simple. It’s just letting the universal place do the universal thing.

Good at optimization, good at summary.

Return to simplicity, return to simplicity.

Simple and clear, layered design.

It begins with simplicity and ends with grace.