Nine Niche Tool Station
Back to List

QA must learn Python scripting practical guide: automated testing, data processing, and report generation all at once

A complete tutorial for QA engineers to use Python to improve efficiency, covering practical scenarios such as automated scripts, test data generation, log analysis, report automation, etc., with directly applicable code examples.

Python QA automation script Test data Report generation pytest

Last Updated:2026-05-24

1. QA How far does it take to learn Python?

QA does not need to be as proficient in Python as developers. What you need is to be able to write scripts to solve repetitive tasks. Here are the levels of Python skills a QA needs to master.

  • Level 1 (Learned In One Week): Variables, Strings, Lists, Dictionaries, If/For Loops, Functions → Able To Write Simple Data Processing Scripts
  • Level 2 (Learned In Two Weeks): File Reading And Writing, Csv/Json Processing, Requests Suite → Able To Automate Api Testing And Data Processing
  • Level 3 (Learned In One Month): Pytest Framework, Selenium/Playwright → Able To Write Automated Tests
  • Level 4 (Advanced): Pandas Data Analysis, Scheduling (Schedule), And Report Automation → Able To Establish A Complete Testing Tool Chain
  • For Qa, Level 2 Can Solve 80% Of The Problems. No Need To Learn Object-Oriented Or Design Patterns

2. Scenario 1: Automate repetitive testing steps

The most common pain point of QA is "having to do the same thing repeatedly every time for regression." Write scripts in Python to automate these steps.

  • Create Test Accounts In Batches: Use The Requests Suite To Call The Registration Api And Create 50 Test Accounts At A Time Instead Of Manually Registering One By One.
  • Automatically Clean Test Data: After The Test Is Completed, Automatically Call The Api Or Connect To The Database To Clean The Test Data To Ensure That The Next Test Environment Is Clean
  • Batch Verification Api Response: Write A Script To Call 100 Api Endpoints, Automatically Check The Status Code And Necessary Fields, And Run It In 10 Seconds, Which Takes 30 Minutes Of Manual Work
  • Environmental Health Check: Automatically Run Once Every Morning To Check Whether All Services In The Test Environment Are Normal, And Automatically Send Notifications If There Are Abnormalities.

Tip

  • First, list the things you do repeatedly every day, pick the most repetitive ones and start automating them.
  • You don’t need to write it perfectly the first time. Write a running version first, and then slowly optimize it later

3. Scenario 2: Test data generation

Testing requires a large amount of data with different conditions, and manual creation is too slow. Use Python's Faker suite to quickly generate various test materials.

  • Installation: Pip Install Faker
  • Generate Fake Information: From Faker Import Faker; Fake = Faker('Zh_Tw') → Fake.Name() Generates Chinese Name, Fake.Email() Generates Email, Fake.Phone_Number() Generates Phone Number
  • Generate Csv In Batches: Use The Csv Package To Write The Generated Data Into Csv Files For Easy Import Into The Test System
  • Boundary Value Data: Generate Boundary Test Data Such As Extremely Long Strings ('A' * 256), Special Characters, Null Values, Extreme Values, Etc.
  • Data That Conforms To Business Logic: Custom Generator, Such As "Amount Is Between 100-9999, Status Is Randomly Selected From [Pending, Paid, Shipped]"

4. Scenario 3: Log analysis automation

Reading the Log manually is too slow, so use a Python script to automatically analyze the error patterns in the Log.

  • Count Error Types: Read Log Files, Use Regular Expressions To Retrieve Error Messages, Count The Number Of Occurrences Of Each Error, And Sort By Frequency
  • Time Distribution Analysis: Analyze The Time Distribution Of Errors And Find The Pattern Of "The Most Errors Occur At 3Pm Every Day"
  • Cross-Log Correlation: Analyze Access.Log And Error.Log Simultaneously To Find Out "Which Api Requests Triggered Errors"
  • Automatic Alarm: The Script Scans The Log Every 5 Minutes And Automatically Sends A Slack Notification If The Number Of Error Exceeds The Threshold.
  • Export Reports: Export Analysis Results Into Html Or Markdown Reports, With Charts Attached

5. Scenario 4: API test automation

Use Python's requests suite + pytest framework to build API automated tests.

  • Basic Api Test: Import Requests; Response = Requests.Get(Url); Assert Response.Status_Code == 200
  • Post Request Test: Requests.Post(Url, Json=Payload, Headers=Headers) → Test The Api For Creating Resources
  • Response Verification: Use The Jsonschema Suite To Verify Whether The Json Structure Of The Api Response Meets The Specifications.
  • Series Test Process: Create Order → Obtain Order Id → Query With Id → Cancel With Id. Verify Responses At Every Step
  • Parametric Testing: Use Pytest.Mark.Parametrize To Run Multiple Sets Of Test Data At One Time, And Test 20 Scenarios With The Same Test Logic

Tip

  • API test scripts are the best starting point for QA automation - more stable than UI tests and easier to understand than unit tests
  • Put the base URL of the API in the configuration file or environment variable to facilitate switching between different environments (dev/staging/prod)

6. Scenario 5: Test reporting automation

Manually compiling test reports every week is a waste of time. Use Python to automatically collect data, analyze, and produce reports from various sources.

  • Pull Bug Data From Jira/Clickup: Use Api To Get This Week'S Bug List, Status, And Severity Distribution
  • Pull Test Results From Ci/Cd: Parse Junit Xml Or Allure Json Reports To Count Pass And Failed Tests
  • Produce Markdown Reports: Use Python String Formatting To Produce Structured Weekly Reports
  • Add Charts: Use Matplotlib Or Plotly To Generate Trend Charts And Embed Them In Reports
  • Auto-Send: Use Smtplib Or Slack Api To Automatically Send Reports To Your Team

7. Scenario 6: Scheduling and Monitoring

Let the script be executed automatically at regular intervals without having to run it manually every time.

  • Python Scheduling: Use The Schedule Package To Schedule Within Scripts. Schedule.Every(30).Minutes.Do(Health_Check) → Run A Health Check Every 30 Minutes
  • System Scheduling: Use Cron (Linux) Or Job Scheduler (Windows) To Execute Python Scripts Regularly
  • Monitoring Script Example: Call All Key Apis Once An Hour To Check Whether The Response Is Normal And Whether The Response Time Exceeds The Standard, And Send Notifications When Exceptions Occur
  • Environment Comparison: Automatically Compare The Api Responses Of Staging And Production Every Day To Find Out The Differences In The Environment

8. Recommended Kits and Learning Routes

The most commonly used Python packages for QA and learning recommendations.

Kit use Installation instructions
requests HTTP requests (API testing) pip install requests
pytest testing framework pip install pytest
faker Fake information generation pip install faker
pandas data analysis pip install pandas
selenium browser automation pip install selenium
playwright Browser Automation (updated) pip install playwright
allure-pytest test report pip install allure-pytest
schedule Task scheduling pip install schedule
python-dotenv Environmental variable management pip install python-dotenv

Tip

  • Suggested learning order: requests → pytest → faker → pandas → selenium/playwright
  • Don't try to learn all the kits at once. Learn requests + pytest first. Being able to write API tests is already very valuable.

Key Takeaways

  • 1 Python is the most recommended programming language for QA engineers to learn - it has simple syntax, rich packages, and active community
  • 2 This guide focuses on actual QA work scenarios. It does not teach you to learn Python from scratch, but teaches you to use Python to solve QA problems.
  • 3 Covers 6 major scenarios: automated scripts, test data generation, Log analysis, API testing, report generation, and scheduled tasks
  • 4 Each scenario has a code example that can be directly copied and modified.
ℹ️

General Disclaimer

The information provided on this site is for reference only. We do not guarantee its completeness or accuracy. Users should determine the applicability of the information on their own.

Feedback