Skip to main content
Version: 2.0.0

Keploy Integration with Pytest

Pre-requisites

  1. Python 3 and above
  2. Pytest
  3. Keploy

You can get the coverage with Keploy in 2 ways:

  1. Using Keploy Binary
  2. Using Keploy Docker

Using Keploy Binary

First you need to install Keploy's Python SDK:

pip install keploy

Next, create a test file for running Keploy's API tests. You can name the file test_keploy.py, and the contents of the file will be as follows:

from keploy import run
def test_keploy():
run("python3 -m coverage run --data-file=.coverage_data.keploy <command-to-run-your-application>")

We also need a .coveragerc file to ignore the coverage of the libraries that is calculated. The contents of the file will be as follows:

[run]
omit =
/usr/*
sigterm = true

Before starting your application, make sure that the debug mode is set to False in your application, for the coverage library to work properly. It should look something like this:

app.run(host=HOST, port=PORT, debug=False)

Now to run your unit tests with Keploy, you can run the command given below:

keploy test -c "python3 -m coverage run -p --data-file=.coverage.unit -m pytest test_keploy.py <your-unit-test-file>" --delay 10 --coverage

Note: If you face any problems with running the coverage library, you can refer to the documentation for the same here

To combine the coverages and get the reports, you can refer to this section.

Hooray🎉! You've sucessfully got the coverage of your Keploy recorded api tests and unit tests using Pytest.

Using Keploy Docker

Add the following lines to your Dockerfile to install the coverage library and to start the application with the coverage library.

RUN pip3 install coverage
CMD ["python3", "-m", "coverage", "run", "-p", "--data-file=./.coverage.unit", "<command-to-run-your-application>"]

You also need to add a .coveragerc file to ignore the coverage of the libraries used and also enable relative paths to combine the coverage files easily.

[run]
omit =
/usr/*
sigterm = true
relative_files = true

You need to make sure that your present working directory on the host is mounted to the working directory in the docker container. In a docker compose file, it will look something like this:

    volumes:
- .:<working-directory-in-the-container>

To get the coverage of Keploy's API tests, you can run the command given below:

keploy test -c "<command-to-run-your-docker-application>" --containerName=<container-name-on-which-tests-have-been-recorded> --buildDelay 100s --delay 10

Now, to get the coverage of your unit tests, you need to update the run command in your Dockerfile to:

CMD ["python3", "-m", "coverage", "run",  "-p", "--data-file=./.coverage.unit", "-m", "pytest", "test_app.py"]

To get the unit coverage you can either run it by using your normal docker run command, or to run it using Keploy, you can use the command below:

keploy test -c "<command-to-run-your-docker-application>" --containerName=<container-name-on-which-tests-have-been-recorded> --buildDelay 100s --delay 10

Now that you have the coverages of both your unit tests and Keploy's API tests, you can combine them and get the report from here

Combine And Get Report

To combine the coverage from the unit tests, and Keploy's API tests we can use the command below:

python3 -m coverage combine

Make sure to run this command before starting a new test run to avoid getting multiple coverage files.

Finally, to generate the coverage report for the test run, you can run:

python3 -m coverage report

and if you want the coverage in an html file, you can run:

python3 -m coverage html