Package: test-swagger-coverage
swagger-coverageAboutSwagger coverage report helps the QA automation and developer to get a simple API coverage report for endpoints testsInstallationYou can installtest-swagger-coverageviapip_ fromPyPI_::$ pip install test-swagger-coverageor with poetry$ poetry add test-swagger-coverageHow it worksWe take a swagger as data for testing coverage and, based on it, we create a file that will be the settings for our tests. The file can be created automatically or manually.Next, we set up api calls in our tests (we wrap them with decorators, see examples) and at the end of testing we generate html report.
We will check which endpoints were called and what statuses we checked.We can't always trust our swagger, so you can manually set the status of the codes yourself, which need to be checked.ExamplesFirst, we need a link to your swagger. For example,  let's take thishttps://petstore.swagger.io/Next, in our project, we need to create a file describing our endpoints, which our tests will use to generate a coverage report.We can do it automatically via the command line, get json swagger file$ swagger_coverage https://petstore.swagger.io/v2/swagger.jsonResult$ 2022-04-15 11:22:37 INFO Start load swagger swagger_coverage https://petstore.swagger.io/v2/swagger.json
$ 2022-04-15 11:22:38 INFO The swagger report was successfully saved to the folder: /Users/user/Documents/git/python-api-tests/swagger_reportTheswagger_reportdirectory will be created
and adata_swagger.yamlfile will appear inside, which will be the settings for building a test coverage reportThedata_swagger.yamlfile looks something like this...
addPet:
 description: 'Add new pet'
 method: POST
 path: /pet
 statuses:
 - 200
 - 404
 tag: pet
 ...whereregUseris the unique id of our endpointstatusesis a list of statuses that we will check (that they were called).
You can customize this list yourself.Only we will check 201 status, as described in the user registration swagger. So I will add it.Let's create a simple test and build a report. For requests, you will use therequestslibrary.importrequestsfromswagger_coverage.src.coverageimportSwaggerCoveragefromswagger_coverage.src.decoimportswagger# swagger data preparationswagger_url="https://api.swaggerhub.com/apis/berpress/flask-rest-api/1.0.0"api_url="https://api.swaggerhub.com/apis/"path='/report'# path to swagger coverage reportswagger_rep=SwaggerCoverage(api_url=api_url,url=swagger_url,path=path)swagger_rep.create_coverage_data()# function to call a request to the server@swagger("regUser")defregister_user(payload:dict):returnrequests.post('https://stores-tests-api.herokuapp.com/register',json=payload)# testdata={"username":"test2023@test.com","password":"Password"}response=register_user(data)assertresponse.status_code==201# create reportswagger_rep.create_report()swagger data preparation: Prepare our file data_swagger.yaml, it will be created automatically.function to call a request to the server:  We will write a user registration call. Declaring a function with a decorator@swagger("regUser")."regUser"taken from filedata_swagger.yaml, this is unique id of our endpoint.test: run the testcreate report: create a report.After that, in the folderswagger_reportwe will receive a reportindex.html.Let's see itAs you can see, we have increased the counter of verified endpointsIf you usepytest, add this code in conftest.py@pytest.fixture(scope="session",autouse=True)defswagger_checker(request):url=request.config.getoption("--swagger-url")url_api=request.config.getoption("--api-url")path='/report'swagger=SwaggerCoverage(api_url=url_api,url=url,path=path)swagger.create_coverage_data()yieldswagger.create_report()Also, at the end of the report, you can find a table of average request times for routesAlso, at the end of the report, you can find a table of average request times for routesMore example with pytest and API testshttps://github.com/berpress/python-api-testsReport examplehttps://github.com/berpress/python-api-tests/tree/main/swagger_report