How to build an automated testing framework? Easy to implement in 4 steps in one article!

Foreword

Recently, many friends are talking about interface automation testing, so what exactly is interface automation testing? Let’s take a look below to find out. First we have to figure out the following issue.

Why do you need (automated) interface testing?

1. As the complexity of each system continues to rise, the cost of traditional testing methods has increased and testing efficiency has dropped significantly. Interface testing is more stable than UI testing, and it is relatively easy to implement automated continuous integration, which can reduce the time of manual regression testing. cost and shorten the test cycle.

2. Interface testing can be involved in project development earlier. Generally, as long as the interface is defined, code can be written. Functional testing must wait until the system provides a testable interface before it can be carried out.

3. Compared with UI testing (which is troublesome in some test environments), interface testing can cover the underlying code logic more simply and comprehensively, thereby discovering some hidden bugs.

4. From a security perspective, the front-end and back-end frameworks of most systems are now separated. Relying only on the front-end for restrictions can no longer meet the security requirements of the system. The back-end needs to be synchronously controlled, so testing also needs to be verified from the interface level.

5. More and more teams are beginning to accept the highly collaborative, integrated thinking of R&D, testing, operation and maintenance, and delivery advocated by DevOps, which puts forward higher requirements for testing efficiency.

Interface testing principle

Simulate the process in which the client sends a request to the server, the server processes it after receiving it and returns a response to the client, and the client receives the response.

Test Scope

  • Business functions (including whether normal and abnormal scenarios are implemented)
  • Business rules (whether coverage is comprehensive)
  • Parameter verification (whether boundaries and business rules meet requirements)
  • Abnormal scenarios (repeated submission, concurrent submission, transaction interruption, multi-machine environment, large data volume testing)
  • Performance testing (response time, throughput, number of concurrencies, resource requirements)
  • Security testing (authority verification, SQL injection, etc.)

1. Planning ideas for automated testing framework

1. Select language

  • python
  • java

Choose which one you are good at, I recommend python

2. Programming tool selection

  • pycharm
  • vscode

Choose which one you are good at

3. Test framework selection

  • unittest —python’s own testing framework
  • pytest —unittest upgraded version, recommended
  • httprunner
  • rf framework —keywords

4. Report visualization solution selection

  • htmltestrunner
  • beautifulreport
  • allure

5. Continuous integration solution

  • jenkins

6. Warehouse server selection

  • github —The server is abroad
  • gitlab
  • gitee

7. Test management tool selection

  • Zen Tao
  • jira

There are generally two ideas for building an interface automated testing framework:

1. Tool-based

For example: Postman + Newman + Jenkins + Git/svn Jmeter + Ant + Jenkins + Git/svn

2. Code-based

For example: Python + Requests + Pytest + Allure

Personal suggestion: If you are in the learning stage, choose the code-based model. By planning the project and writing code step by step, you can better understand the implementation principles of interface automation. You will be more comfortable learning some tools later.

What I choose here is: Python + pycharm + pytest + allure + gitlab + jira

After planning the plan, we can create our project code project (it can be done in parallel with writing test cases, and the format of the test cases needs to be agreed in advance to facilitate subsequent code design).

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036 [password: csdn999]

2. Project code engineering construction ideas

Principles of design framework:

  • Encapsulate base class method

Some more general methods can be encapsulated, such as sending requests, adding, deleting, modifying, and checking.

  • High cohesion and low coupling

Each module completes its own functions as independently as possible and does not rely on code outside the module.

The complexity of the interface between modules should be as low as possible. For example, the calls between methods should be minimized within the class. Otherwise, changes in one method will affect another method that calls it.

  • Script separation

Business code and test data should be separated from each other and called flexibly. The concept is similar to the PO design pattern that was first introduced to the PO pattern and simply practiced in Selenium. Specific data and configuration should not appear in the code. Instead, the corresponding data file is called.

3. A relatively complete project code engineering structure:

- common #Package file, public module, stores some common methods
    -baseapi.py
        - class BaseApi()#base class
            - Method 1: Send request
            - Method 2: Increase
            - Method 3: Delete
            - Method 4: Change
            - Method 5: Check
- libs #package file, which stores business layer code
    - login.py #Login module
        - class Login(BaseApi) #Inherit BaseApi in the base class
            - Method 1: Send login request
            - Method 2: Send a logout request
    - logout.py #Logout module
        - class Logout(BaseApi)
- configs #Package file, store configuration
    -config.py
        - HOST='xxx'# is used to switch the test environment
        - url='xxx'
- datas #folder to store data/test cases
    -xxx.xls
    -xxx.yaml
- testCase #Package file to store test case code, please pay attention to comply with the pytest naming convention
    - test_login.py
        - class Test_login
            - Method 1: test_login01
            - Method 2: test_login02
    - test_logout.py
        - - class Test_logout
            - Method 1: test_logout01
            - Method 2: test_logout02
- outFiles #folder, output file
    - logs #store log files
    - report #Storage report
    - screenShot #Save screenshots
- tools #package file, tool class
    - handle_data.py
    - handle_excel.py
    - handle_path.py
    - handle_yaml.py
- docs #folder to store documentation
    - Code specification.doc
    - Requirements document.doc

Framework construction:

4. Ideas for subsequent code writing:

The code writing ideas after the framework is written are generally as follows

1. Base class encapsulation, putting some commonly used methods such as sending requests, adding, deleting, modifying, and searching into our base class.

2. Write the interface code of the business layer

3. Write the test case code. During the process, if you find anything missing, write a method. Think about whether this method should be placed in the specific business, the base class, or the tools. This process is a process of continuous optimization of the code. Until our use case code is finished.

  • For example, if you need to read a yaml file when writing test case code, add a get_yml_data method in tools.
  • For another example, if two business modules need to be related, and method A needs to return an object for method B to use, then method A should be optimized to give a return value.
  • For another example, if some key nodes need to be screenshotted, then supplement the screenshot method.

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 388,465 people are learning the system