Public account concerns: test charging treasure, communication together

Pytest-html generates reports

Pytest uses the Pytest-HTML plug-in to generate test reports without writing your own report generation code.

  • Installation:pip install pytest-html
  • Execution method:pytest --html=./reports/report.html

Test code:

import pytest


def login(username, password) :
    """ Simulated login """
    user = "Linux super"
    pwd = "Linux ultra brother"
    if user == username and pwd == password:
        return {"code": 1001."msg": "Login successful"."data": None}
    else:
        return {"code": 1000."msg": "Wrong username or password"."data": None}


test_data = [
    # Test data
    {
        "case": "Correct username, correct password"."user": "Linux super"."pwd": "Linux ultra brother"."expected": {"code": 1001."msg": "Login successful"."data": None}}, {"case": "User name correct, password blank"."user": "Linux super"."pwd": ""."expected": {"code": 1000."msg": "Wrong username or password"."data": None}}, {"case": "Empty username, correct password"."user": ""."pwd": "Linux ultra brother"."expected": {"code": 1000."msg": "Wrong username or password"."data": None}}, {"case": "Wrong username, wrong password"."user": "linux"."pwd": "linux"."expected": {"code": 1000."msg": "Wrong username or password"."data": None}}]class TestLogin(object) :

    @pytest.mark.parametrize("data", test_data)
    def test_login(self, data) :
        result = login(data["user"], data["pwd"])
        assert result == data["expected"]


if __name__ == '__main__':
    pytest.main(['-sv'."test_pytest_html"."--html"."./reports/report.html"])
Copy the code

Pytest test_pytest_html.py — HTML =./reports/report. HTML generates a report in the Reports folder, as shown below

Pytest integration allure

1. Download and install all

Allure download the latest version: github.com/allure-fram…

Unzip the download when it’s finished. Then set the environment variable to allure’s bin directory. CMD type allure to check whether the environment variable was set successfully.

2. Allure – pytest installation

Download the allure- PyTest plugin to generate the data needed for the Allure test report.

pip install allure-pytest

3. The code is as follows

import pytest
import allure

@pytest.fixture(scope='function')
def login() :
    print("Login")
    yield
    print("Login completed")

@allure.feature('Add to cart')
def test_1(login) :
    "Add apples to cart"
    print("Test Case 1")

@allure.feature('Add to cart')
def test_2() :
    Add oranges to cart
    print("Test Case 2")

if __name__ =="__main__":
    The data needed to generate the Allure report is stored in the /temp directory
    pytest.main(['--alluredir'.'./temp'])
Copy the code

Some feature points in the @allure decorator:

  • @allure. Feature: Used to define the feature being tested, the need point of the product being tested
  • @allure. Story: Used to define the user story of the feature under test, the sub-feature points
  • Step: Used to break a test case into several steps to output in a report
  • @allure. Attach: Used to attach additional information to the test report, usually test data

Running the test file generates the data required for the report in the Temp folder

Execute the following command in the current directory to generate the Allure report

allure generate ./temp -o ./report –clean

And you’re done!

At this point, the introduction to PyTest is over, and it is up to you to explore it further at work. This series is just an overview