xiaoli-autotest-pytest 1.0.1
Simple autotest framework, using python and pytest as the basis for data comparison, interface testing, UI testing and so on
License: MIT
Keywords: example project



## Contents

- [Installation](#Installation)
- [Usage](#Usage)
- [Report](#Report)
- [Docker](#Docker)
- [DevOps](#DevOps)

## Installation

```commandline
pip install xiaoli-autotest-pytest
```

## Usage
#### 1. cli-run
```
pytest --csv=script/test_hello.csv --slowtime=1000 --headless

cases/test_main.py::test_http_api[Print hello]
Test Case => {'case_id': '0001', 'case_name': 'Test hello', 'step_name': 'Print hello', 'protocols': 'http_api', 'route': 'mods.hello.hello.print_hello', 'parameter': '', 'scope': 'dev'}
 => Load route mods.hello.hello success
 => Load class Hello success
 => Load cases print_hello success
hello
PASSED
```

#### 2. Parameter Description
| Parameters |  Type   | Required | Description                                                                             |
|:-----------|:-------:|:--------:|:----------------------------------------------------------------------------------------|
| --csv      |   str   |   Yes    | Your script file name                                                                   |
| --slowtime |   int   |    No    | The interval of each step of the UI test, in milliseconds, the default 1000ms is 1 second|
| --headless | boolean |    No    | Whether to enable headless mode (no browser mode) during UI test|

#### 3. Test Case Script
Create a csv script file in the script directory, e.g. <test_hello.csv>
```Excel
case_id,case_name,step_id,step_name,method,route,parameter,hard_assert,scope
0001,Test hello,1,Print hello,test_http_api,hello.hello.print_hello,,false,dev
```

> Note: It can be created or opened in Excel, and must be saved in "CSV UTF-8(Comma delimited)" format to avoid garbled Chinese characters. Save as "UTF-8" when using other text tools

#### 4. Test Case Field Description
| Parameters | Type | Required | Description                                                                    |
|:-----------|:-----|:--------:|:-------------------------------------------------------------------------------|
| case_id    | str  |   Yes    | Test case number, unique                                                       |
| case_name  | str  |   Yes    | Test case name                                                                 |
| step_name  | str  |   Yes    | Test case step                                                                 |
| protocols  | str  |   Yes    | Test Protocol<br/>http_api: http interface test<br/>playwright: UI test driven |
| route      | str  |   Yes    | Test class-to-method routing, all lowercase underscores                        |
| parameter  | str  |    No    | Test parameter                                                                 |
| scope      | str  |   Yes    | Test scope, default: smoke, dev, test, stage, etc.                             |

## Report
allure-results is generated by default under the project root directory. The development environment does not automatically generate Allure Report HTML, you need to manually generate and open the html report:

```commandline
allure serve allure-results
```

## Docker

#### 1. docker pull
```commandline
docker push roger813/xiaoli-autotest-pytest:tag
```

#### 2. rename image
> Note: This step is optional, if you do not change the name, then the image name in the next step needs to be replaced with the full name

```commandline
docker tag roger813/xiaoli-autotest-pytest:tag xiaoli-autotest-pytest
```

#### 3. verify image 
```commandline
docker run -d --name pytest -p 8080:8080 xiaoli-autotest-pytest
```
- Open the container and the log is displayed: Report successfully generated to allure-report, Images are available.
- Open your browser to access the allure report: http://localhost:8080
- If the following page is displayed, the image is successfully pulled

![allure_demo.png](xiaoli-autotest-pytest/assets/allure_demo.png)

#### 4. business Test
```text
cd your-project-directory
docker run -d --name pytest -p 8080:8080 -v %cd%\assets:/work/assets -v %cd%\confs:/work/confs -v %cd%\mods:/work/mods -v %cd%\script:/work/script -e csv=custom.csv swpic-autotest-pytest
```
- Go to your project directory
- Mount the assets, confs, mods, and script directories in the service project
- Add environmental parameters
  - -e csv=your_script_file.csv(required)
  - -e scope=dev(optional), Determine your test scope, default to full environment testing
- Open your browser to access the allure report: http://localhost:8080

## DevOps
#### Jenkins Pipeline
```groovy
parameters {
        string(
            name: 'SCRIPT',
            defaultValue: 'your_test_script.csv',
            description: '测试脚本CSV文件, 描述测试业务逻辑(必须)')
        string(
            name: 'TEST_SCOPE',
            defaultValue: 'dev,test,stage,smoke,regress',
            description: '测试范围, 默认全范围测试. 选择需要测试的用例范围(可选)')
        // string(
        //     name: 'SLOW_TIME',
        //     defaultValue: '1000',
        //     description: '测试间隔, UI测试中每一步操作间隔时间, 单位毫秒(可选)')
        string(
            name: 'BUSINESS_BRANCH',
            defaultValue: 'dev',
            description: '测试代码仓库分支(可选)')
        booleanParam(
            name: 'PARALLEL_TEST',
            defaultValue: false,
            description: '是否多线程, 默认关闭. 适用于没有依赖关系的用例集(可选)')
    }
```

| Parameters      | Description                     | Required | Proposal                                                                         |
|:----------------|:--------------------------------|:---------|:---------------------------------------------------------------------------------|
| SCRIPT          | your_script_file                | Yes      |                                                                                  |
| TEST_SCOPE      | test scope                      | Yes      |                                                                                  |
| SLOW_TIME       | test interval                   | No       | If it is not a UI test project, you do not need it                               |
| BUSINESS_BRANCH | Branch of the business repo     | Yes      | Quickly verify branch correctness and avoid unnecessary merging efforts          |
| PARALLEL_TEST   | Whether to enable multithreading| No       | Enable this, speed up the test, use cases have dependencies and use with caution |

#### run with parameter
![jenkins_run_entry.png](xiaoli-autotest-pytest/assets/jenkins_run_entry.png)
![jenkins_run_config.png](xiaoli-autotest-pytest/assets/jenkins_run_config.png)

#### view logs & reports
![jenkins_run_logs.png](xiaoli-autotest-pytest/assets/jenkins_run_logs.png)
![jenkins_report_entry.png](xiaoli-autotest-pytest/assets/jenkins_report_entry.png)
![jenkins_allure_report.png](xiaoli-autotest-pytest/assets/jenkins_allure_report.png)

## End