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.
Owner
Karma Computing
Karma Computing
Tools for test driven data-wrangling and data validation.

datatest: Test driven data-wrangling and data validation Datatest helps to speed up and formalize data-wrangling and data validation tasks. It impleme

269 Dec 16, 2022
a socket mock framework - for all kinds of socket animals, web-clients included

mocket /mɔˈkɛt/ A socket mock framework for all kinds of socket animals, web-clients included - with gevent/asyncio/SSL support ...and then MicroPytho

Giorgio Salluzzo 249 Dec 14, 2022
pywinauto is a set of python modules to automate the Microsoft Windows GUI

pywinauto is a set of python modules to automate the Microsoft Windows GUI. At its simplest it allows you to send mouse and keyboard actions to windows dialogs and controls, but it has support for mo

3.8k Jan 06, 2023
A twitter bot that simply replies with a beautiful screenshot of the tweet, powered by poet.so

Poet this! Replies with a beautiful screenshot of the tweet, powered by poet.so Installation git clone https://github.com/dhravya/poet-this.git cd po

Dhravya Shah 30 Dec 04, 2022
✅ Python web automation and testing. 🚀 Fast, easy, reliable. 💠

Build fast, reliable, end-to-end tests. SeleniumBase is a Python framework for web automation, end-to-end testing, and more. Tests are run with "pytes

SeleniumBase 3k Jan 04, 2023
Minimal example of how to use pytest with automated 'devops' style automated test runs

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 m

Karma Computing 2 Jan 02, 2022
Faker is a Python package that generates fake data for you.

Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in yo

Daniele Faraglia 15.2k Jan 01, 2023
Scalable user load testing tool written in Python

Locust Locust is an easy to use, scriptable and scalable performance testing tool. You define the behaviour of your users in regular Python code, inst

Locust.io 20.4k Jan 04, 2023
The source code and slide for my talk about the subject: unittesing in python

PyTest Talk This talk give you some ideals about the purpose of unittest? how to write good unittest? how to use pytest framework? and show you the ba

nguyenlm 3 Jan 18, 2022
Multi-asset backtesting framework. An intuitive API lets analysts try out their strategies right away

Multi-asset backtesting framework. An intuitive API lets analysts try out their strategies right away. Fast execution of profit-take/loss-cut orders is built-in. Seamless with Pandas.

Epymetheus 39 Jan 06, 2023
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 08, 2022
Automated Penetration Testing Framework

Automated Penetration Testing Framework

OWASP 2.1k Jan 01, 2023
模仿 USTC CAS 的程序,用于开发校内网站应用的本地调试。

ustc-cas-mock 模仿 USTC CAS 的程序,用于开发校内网站应用阶段调试。 请勿在生产环境部署! 只测试了最常用的三个 CAS route: /login /serviceValidate(验证 CAS ticket) /logout 没有测试过 proxy ticket。(因为我

taoky 4 Jan 27, 2022
Given some test cases, this program automatically queries the oracle and tests your Cshanty compiler!

The Diviner A complement to The Oracle for compilers class. Given some test cases, this program automatically queries the oracle and tests your compil

Grant Holmes 2 Jan 29, 2022
Useful additions to Django's default TestCase

django-test-plus Useful additions to Django's default TestCase from REVSYS Rationale Let's face it, writing tests isn't always fun. Part of the reason

REVSYS 546 Dec 22, 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
A simple tool to test internet stability.

pingtest Description A personal project for testing internet stability, intended for use in Linux and Windows.

chris 0 Oct 17, 2021
Compiles python selenium script to be a Window's executable

Problem Statement Setting up a Python project can be frustrating for non-developers. From downloading the right version of python, setting up virtual

Jerry Ng 8 Jan 09, 2023
A simple asynchronous TCP/IP Connect Port Scanner in Python 3

Python 3 Asynchronous TCP/IP Connect Port Scanner A simple pure-Python TCP Connect port scanner. This application leverages the use of Python's Standa

70 Jan 03, 2023