|
1 | 1 | # swagger-coverage-py
|
2 |
| -Adaptation of swagger-coverage project for python |
3 |
| -... |
4 | 2 |
|
5 |
| -The description is going to be here a bit later |
| 3 | +### This project is the adapter which allows using [swagger-coverage](https://github.com/viclovsky/swagger-coverage) tool in Python projects (PyTest+Requests). |
| 4 | + |
| 5 | +## Original description summary: |
| 6 | + |
| 7 | +> Swagger-coverage gives a full picture about coverage of API tests (regression) based on OAS 2 (Swagger). By saying coverage we mean not a broad theme functionality, but presence (or absence) of calls defined by API methods, parameters, return codes or other conditions which corresponds specification of API. |
| 8 | +
|
| 9 | +Some more info about the project you can also |
| 10 | +find [HERE](https://viclovsky.github.io/%D0%B0%D0%B2%D1%82%D0%BE%D1%82%D0%B5%D1%81%D1%82%D1%8B%20%D0%BD%D0%B0%20api/2020/01/16/swagger-coverage) |
| 11 | +. |
| 12 | +<br><img src="https://raw.githubusercontent.com/JamalZeynalov/wagger-coverage-py/master/images/swagger-coverage-report.png" width=1100 alt="Report"> |
| 13 | + |
| 14 | +# How to use: |
| 15 | + |
| 16 | +### 1. Create and place `swagger-coverage-config.json` file to your project: |
| 17 | + |
| 18 | +Examples of configuration options you can find |
| 19 | +in [Configuration options](https://github.com/viclovsky/swagger-coverage#configuration-options) section of the original |
| 20 | +tool documentation. |
| 21 | +> #### Note: You can change the name and location of this file if you want. |
| 22 | +
|
| 23 | +### 2. Create `swagger-coverage-adapter-config.json` file in your project root: |
| 24 | + |
| 25 | +```.json |
| 26 | +{ |
| 27 | + "output_dir": "swagger-coverage-output", |
| 28 | + "swagger_coverage_config": "swagger-coverage-config.json", |
| 29 | + "link_to_swagger_json": "https://petstore.swagger.io/v2/swagger.json", |
| 30 | + "ignore_requests": [ |
| 31 | + "*any-part-of-request-name*", |
| 32 | + ] |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +* **output_dir** - specify any path (from your project root) for your output folder. The tool will automatically create |
| 37 | + this folder and save all recorded JSON files there. |
| 38 | +* **swagger_coverage_config** - Path to your `swagger-coverage-config.json` file |
| 39 | +* **link_to_swagger_json** - HTTP(s) link to your OpenApi/Swagger documentation in JSON format. Adapted `swagger.json` |
| 40 | + file will be created in your project root. |
| 41 | +* **ignore_requests** - all files matching any of listed masks will be removed before the report generation. |
| 42 | + |
| 43 | +### 3. Add the session scoped fixture |
| 44 | + |
| 45 | +```python |
| 46 | +@pytest.fixture(scope="session", autouse=True) |
| 47 | +def setup_swagger_coverage(): |
| 48 | + runner = Runner() |
| 49 | + runner.cleanup_input_files() |
| 50 | + runner.setup() |
| 51 | + yield |
| 52 | + runner.generate_report() |
| 53 | +``` |
| 54 | + |
| 55 | +> #### Note: If your API requires authentication then pass auth as an argument. |
| 56 | +> #### Example: |
| 57 | +> ```python |
| 58 | +> runner.setup(auth=HTTPBasicAuth("username", "password")) |
| 59 | +> ``` |
| 60 | +> This is required to download swagger.json |
| 61 | +
|
| 62 | +### 3. Add the session scoped fixture |
| 63 | +
|
| 64 | +```python |
| 65 | +from requests import Response |
| 66 | +from requests.auth import HTTPBasicAuth |
| 67 | +from swagger_coverage_py.listener import CoverageListener |
| 68 | +
|
| 69 | +response: Response = CoverageListener( |
| 70 | + method="get", |
| 71 | + base_url="https://petstore.swagger.io", |
| 72 | + raw_path="/v2/store/order/{orderId}", |
| 73 | + uri_params={"orderId": 1}, |
| 74 | + auth=HTTPBasicAuth("username", "password"), |
| 75 | + params={"type": "active"}, |
| 76 | +).response |
| 77 | +``` |
| 78 | +> #### Note: "auth" and "params" arguments are not required. <br>You can use any other **kwargs that are applicable for Requests library. |
| 79 | +
|
| 80 | +# How it works: |
| 81 | +0. The fixture `setup_swagger_coverage` setups required artifacts |
| 82 | +1. During test execution the CoverageListener saves all requests as JSON files in swagger format. |
| 83 | +2. After the test execution the `Runner().generate_report()` creates a new report and saves it into your project root. |
| 84 | + |
| 85 | +> #### Note: The `swagger-coverage-report.html` file depends on `swagger-coverage-results.json`. |
| 86 | +> #### You should keep them together if you want to send this report via email. |
0 commit comments