Minimal example of how to use pytest with automated 'devops' style automated test runs

Overview

Pytest python example with automated testing

This is a minimal viable example of pytest with an automated run of tests for every push/merge into the main branch and a auto schedule to run the tests every day.

It also shows an example of how to handle sensitive information (such as api keys) in your pipeline using secrets.

How does this work?

  • At every push and/or merge to the main branch, pytest is used to run the tests which are written in the ./tests directory of this repo
  • The exact same tests may be ran locally (see "Run tests locally")
  • Automated testing with a pipline and secrets (using Github actions - but this process is the same for any provider; Gitlab, bitbucket etc) see https://github.com/KarmaComputing/pytest-example-automated-testing/actions

Setup (local)

python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt

Copy api key (this is an example , it's not a real api key-you can use any value).

cp .env.example .env

Run tests locally

. venv/bin/activate
python -m pytest -vv

Example Output (when tests pass)

collected 1 item                                                                                                                  

tests/test_app.py::test_add PASSED                                                                                          [100%]

======================================================== 1 passed in 0.01s ========================================================
(venv) (base) $

Example Output (when a test fails)


Run python -m pytest -vv
============================= test session starts ==============================
platform linux -- Python 3.10.1, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /opt/hostedtoolcache/Python/3.10.1/x64/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/work/pytest-example-automated-testing/pytest-example-automated-testing
collecting ... collected 2 items

tests/test_app.py::test_add PASSED                                       [ 50%]
tests/test_app.py::test_get_api_key FAILED                               [100%]

=================================== FAILURES ===================================
_______________________________ test_get_api_key _______________________________

    def test_get_api_key():
>       assert app.get_api_key() is not None

tests/test_app.py:9: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def get_api_key():
>       assert API_KEY is not None, "An empty API_KEY should never be allowed, did you forget to either add it to your .env file or, set the environment variable?"  # noqa: E501
E       AssertionError: An empty API_KEY should never be allowed, did you forget to either add it to your .env file or, set the environment variable?

app/app.py:13: AssertionError
=========================== short test summary info ============================
FAILED tests/test_app.py::test_get_api_key - AssertionError: An empty API_KEY...
========================= 1 failed, 1 passed in 0.04s ==========================
Error: Process completed with exit code 1.
You might also like...
pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files
pytest plugin that let you automate actions and assertions with test metrics reporting executing plain YAML files

pytest-play pytest-play is a codeless, generic, pluggable and extensible automation tool, not necessarily test automation only, based on the fantastic

A set of pytest fixtures to test Flask applications

pytest-flask An extension of pytest test runner which provides a set of useful tools to simplify testing and development of the Flask extensions and a

pytest plugin for a better developer experience when working with the PyTorch test suite

pytest-pytorch What is it? pytest-pytorch is a lightweight pytest-plugin that enhances the developer experience when working with the PyTorch test sui

API Test Automation with Requests and Pytest

api-testing-requests-pytest Install Make sure you have Python 3 installed on your machine. Then: 1.Install pipenv sudo apt-get install pipenv 2.Go to

A pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database
A pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database

This is a pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database. It allows you to specify fixtures for PostgreSQL process and client.

A pytest plugin that enables you to test your code that relies on a running Elasticsearch search engine
A pytest plugin that enables you to test your code that relies on a running Elasticsearch search engine

pytest-elasticsearch What is this? This is a pytest plugin that enables you to test your code that relies on a running Elasticsearch search engine. It

a wrapper around pytest for executing tests to look for test flakiness and runtime regression

bubblewrap a wrapper around pytest for assessing flakiness and runtime regressions a cs implementations practice project How to Run: First, install de

Front End Test Automation with Pytest Framework

Front End Test Automation Framework with Pytest Installation and running instructions: 1. To install the framework on your local machine: clone the re

pytest_pyramid provides basic fixtures for testing pyramid applications with pytest test suite

pytest_pyramid pytest_pyramid provides basic fixtures for testing pyramid applications with pytest test suite. By default, pytest_pyramid will create

Owner
Karma Computing
Karma Computing
ApiPy was created for api testing with Python pytest framework which has also requests, assertpy and pytest-html-reporter libraries.

ApiPy was created for api testing with Python pytest framework which has also requests, assertpy and pytest-html-reporter libraries. With this f

Mustafa 1 Jul 11, 2022
Playwright Python tool practice pytest pytest-bdd screen-play page-object allure cucumber-report

pytest-ui-automatic Playwright Python tool practice pytest pytest-bdd screen-play page-object allure cucumber-report How to run Run tests execute_test

moyu6027 11 Nov 8, 2022
pytest plugin providing a function to check if pytest is running.

pytest-is-running pytest plugin providing a function to check if pytest is running. Installation Install with: python -m pip install pytest-is-running

Adam Johnson 21 Nov 1, 2022
Pytest-rich - Pytest + rich integration (proof of concept)

pytest-rich Leverage rich for richer test session output. This plugin is not pub

Bruno Oliveira 170 Dec 2, 2022
A pytest plugin to run an ansible collection's unit tests with pytest.

pytest-ansible-units An experimental pytest plugin to run an ansible collection's unit tests with pytest. Description pytest-ansible-units is a pytest

Community managed Ansible repositories 9 Dec 9, 2022
A small automated test structure using python to test *.cpp codes

Get Started Insert C++ Codes Add Test Code Run Test Samples Check Coverages Insert C++ Codes you can easily add c++ files in /inputs directory there i

Alireza Zahiri 2 Aug 3, 2022
A set of pytest fixtures to test Flask applications

pytest-flask An extension of pytest test runner which provides a set of useful tools to simplify testing and development of the Flask extensions and a

pytest-dev 433 Dec 23, 2022
Selects tests affected by changed files. Continous test runner when used with pytest-watch.

This is a pytest plug-in which automatically selects and re-executes only tests affected by recent changes. How is this possible in dynamic language l

Tibor Arpas 614 Dec 30, 2022
Local continuous test runner with pytest and watchdog.

pytest-watch -- Continuous pytest runner pytest-watch a zero-config CLI tool that runs pytest, and re-runs it when a file in your project changes. It

Joe Esposito 675 Dec 23, 2022
pytest plugin for manipulating test data directories and files

pytest-datadir pytest plugin for manipulating test data directories and files. Usage pytest-datadir will look up for a directory with the name of your

Gabriel Reis 191 Dec 21, 2022