To test an python app is necessary to use some test toll like nose or Pytest. Ins this example we use PyTest for simplicity.
Pytest searches for tests based on the naming convention in the test files:
-
Test cases are functions with name starting with
test_ -
Test files should start with
test_ -
Classes with tests should end with
_Tests
All test cases should consist at least of an assertion
def test_pass():
assert TrueIf you want to use your own name convention or configure other properties you can do it by creating a pytest.ini in the root of the test folder.
In this example I just configure the junit report to generate files in the old format to be used on CI-CD tools
# content of pytest.ini
[pytest]
junit_family=legacyIts a good practicie use a exclusive requiremts file for the testing because these tools should not be package together with the application.
So we create a second requirements file in the test folder.
bandit==1.7.0
pylama==7.7.1
pytest==6.2.5
pytest-xdist==2.4.0The contents of this file are packages used to run the tests (pytest and pytest-xdist) and verify the code (pylama and bandit).
On the Dockerfile we create a new layer reusing the requirement layer and naming it as test. Then we copy the app code, install the test requirements and execute the tests:
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt && pip check
FROM requirements as test
COPY src /app
RUN pip install -r /test/requirements.txt
RUN pytest -n 4The command who execute the tests is
RUN pytest --junit-xml=/reports/unit.xmlIt calls the pytest packaging passing the option --junit-xml that generate a report in the given path.
The pytest command searches for python tests and find a simple test we added before.