Python automated testing practice

Interface automated testing refers to simulating user behavior by writing programs to automatically test interfaces. Python is a popular programming language that is widely used in interface automation testing. The following is a detailed introduction to the actual implementation of automated testing of Python interfaces.

1. Interface automated testing framework

In Python interface automated testing, we can use many open source testing frameworks, such as unittest, pytest, nose, etc. These frameworks provide very rich functions to support interface automation testing.

2. Send HTTP request

In interface automation testing, we need to send HTTP requests to simulate user behavior. Python provides the requests library to easily send HTTP requests. Here's a simple example:

import requests
 
response = requests.get('http://www.example.com')
print(response.status_code)
print(response.text)

In the above code, we use the requests library to send a GET request and print out the response status code and content.

At the same time, I have also prepared a software testing video tutorial for everyone (including interviews, interfaces, automation, performance testing, etc.), which is below. If you need it, you can watch it directly, or you can directly click on the small card at the end of the article to get the information document for free.

Where to watch software testing video tutorials:

Byte boss teaches you how to master automated testing (interface automation/APP automation/Web automation/performance testing) within 15 days, including practical project practice

3. Parse JSON data

In interface automation testing, the returned data is usually in JSON format. Python provides the json library to parse JSON data. Here's a simple example:

import requests
import json
 
response = requests.get('http://www.example.com/api/users')
users = json.loads(response.text)
for user in users:
    print(user['name'])

In the above code, we send a GET request, obtain all user information, and use the json library to parse the returned JSON data.

4. Assert test results

In interface automated testing, we need to verify the returned data. Typically we use assertions to verify that test results are as expected. Here's a simple example:

import requests
import json
 
response = requests.get('http://www.example.com/api/users')
users = json.loads(response.text)
 
assert len(users) == 3
for user in users:
    assert 'name' in user
    assert 'age' in user

In the above code, we use multiple assertions to verify whether the returned user information meets expectations.

5. Data-driven testing

In interface automated testing, we usually need to test different interfaces and parameters. Using data-driven testing makes it easy to execute a large number of test cases. Here's a simple example:

import requests
import json
import unittest
 
class TestUsers(unittest.TestCase):
    def test_users(self):
        url = 'http://www.example.com/api/users'
        params = {'page': 1, 'count': 10}
        response = requests.get(url, params=params)
        users = json.loads(response.text)
        
        self.assertEqual(len(users), 10)
        for user in users:
            self.assertIn('name', user)
            self.assertIn('age', user)
 
if __name__ == '__main__':
    unittest.main()

In the above code, we use the unittest framework to write a test case and execute multiple tests using data-driven testing.

Summarize:

Python interface automated testing is a very important technology that can help us quickly and accurately verify the correctness of the API. We need to master basic knowledge such as Python programming language, HTTP protocol and JSON data format, as well as use tools such as requests library, json library and unittest framework to conduct testing.

PS: Here is a collection of self-study tutorials for software testing. It should be helpful to friends who are developing in the testing industry. If you need it, you can dd me. In addition to basic introductory resources, bloggers also collect a lot of advanced automation resources. From theory to practice, only by integrating knowledge and action can you truly master it. The full set of content has been packaged on the network disk, and the total content is close to 500 G.

☑ 240 episodes - a complete set of video courses from zero to mastery
☑ [Courseware + Source Code] - complete supporting tutorials
☑ 18 sets - source code of practical testing projects
☑ 37 sets - testing tool software package
☑ 268 - real interview questions
☑ 200 templates - Interview resume template, test plan template, software test report template, test analysis template, test plan template, performance test report, performance test report, performance test script case template (complete information)

These materials should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! Everything must be done early, especially in the technical industry, where technical skills must be improved.

Guess you like

Origin blog.csdn.net/huace3852/article/details/132906111