Redis fixtures and fixture factories for Pytest.

Overview

https://raw.githubusercontent.com/ClearcodeHQ/pytest-redis/master/logo.png

pytest-redis

Latest PyPI version Wheel Status Supported Python Versions License

What is this?

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

How to use

Plugin contains three fixtures

  • redisdb - This is a redis client fixture. It constructs a redis client and cleans redis database after the test.
    It relies on redis_proc fixture, and as such the redis process is started at the very beginning of the first test using this fixture, and stopped after the last test finishes.
  • redis_proc - session scoped fixture, that starts Redis instance at it's first use and stops at the end of the tests.
  • redis_nooproc - a nooprocess fixture, that's connecting to already running redis

Simply include one of these fixtures into your tests fixture list.

#
def test_redis(redisdb):
    """Check that it's actually working on redis database."""
    redisdb.set('test1', 'test')
    redisdb.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redisdb.get("did_it") == 1

For the example above works like following:

  1. pytest runs tests
  2. redis_proc starts redis database server
  3. redisdb creates client connection to the server
  4. test itself runs and finishes
  5. redisdb cleans up the redis
  6. redis_proc stops server (if that was the last test using it)
  7. pytest ends running tests

You can also create additional redis client and process fixtures if you'd need to:

from pytest_redis import factories

redis_my_proc = factories.redis_proc(port=None)
redis_my = factories.redisdb('redis_my_proc')

def test_my_redis(redis_my):
    """Check that it's actually working on redis database."""
    redis_my.set('test1', 'test')
    redis_my.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redis_my.get("did_it") == 1

Note

Each Redis process fixture can be configured in a different way than the others through the fixture factory arguments.

Connecting to already existing redis database

Some projects are using already running redis servers (ie on docker instances). In order to connect to them, one would be using the redis_nooproc fixture.

redis_external = factories.redisdb('redis_nooproc')

def test_redis(redis_external):
    """Check that it's actually working on redis database."""
    redis_external.set('test1', 'test')
    redis_external.set('test2', 'test')

    my_functionality = MyRedisBasedComponent()
    my_functionality.do_something()
    assert my_functionality.did_something

    assert redis_external.get("did_it") == 1

By default the redis_nooproc fixture would connect to Redis instance using 6379 port. Standard configuration options apply to it.

These are the configuration options that are working on all levels with the redis_nooproc fixture:

Configuration

You can define your settings in three ways, it's fixture factory argument, command line option and pytest.ini configuration option. You can pick which you prefer, but remember that these settings are handled in the following order:

  • Fixture factory argument
  • Command line option
  • Configuration option in your pytest.ini file
Configuration options
Redis server option Fixture factory argument Command line option pytest.ini option Noop process fixture Default
executable executable --redis-exec redis_exec
/usr/bin/redis-server
host host --redis-host redis_host host 127.0.0.1
port port --redis-port redis_port port random
connection timeout timeout --redis-timeout redis_timeout
30
number of databases db_count --redis-db-count redis_db_count
8
Whether to enable logging to the system logger syslog --redis-syslog redis_syslog
False
Redis log verbosity level loglevel --redis-loglevel redis_loglevel
notice
Compress dump files compress --redis-compress redis_compress
True
Add checksum to RDB files checksum --redis-rdbcompress redis_rdbchecksum
False
Save configuration save --redis-save redis_save
""
Redis test instance data directory path datadir --redis-datadir redis_datadir
""

Example usage:

  • pass it as an argument in your own fixture

    redis_proc = factories.redis_proc(port=8888)
  • use --redis-port command line option when you run your tests

    py.test tests --redis-port=8888
    
  • specify your port as redis_port in your pytest.ini file.

    To do so, put a line like the following under the [pytest] section of your pytest.ini:

    [pytest]
    redis_port = 8888

Options below are for configuring redis client fixture.

Redis client option Fixture factory argument Command line option pytest.ini option Default
decode_response decode --redis-decode redis_decode False

Package resources

Owner
Clearcode
Software house with a passion for technology. We specialize in building enterprise-grade adtech, martech and analytics platforms.
Clearcode
Pytest-typechecker - Pytest plugin to test how type checkers respond to code

pytest-typechecker this is a plugin for pytest that allows you to create tests t

vivax 2 Aug 20, 2022
An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.

mitmproxy mitmproxy is an interactive, SSL/TLS-capable intercepting proxy with a console interface for HTTP/1, HTTP/2, and WebSockets. mitmdump is the

mitmproxy 29.7k Jan 02, 2023
Ab testing - The using AB test to test of difference of conversion rate

Facebook recently introduced a new type of offer that is an alternative to the current type of bidding called maximum bidding he introduced average bidding.

5 Nov 21, 2022
PoC getting concret intel with chardet and charset-normalizer

aiohttp with charset-normalizer Context aiohttp.TCPConnector(limit=16) alpine linux nginx 1.21 python 3.9 aiohttp dev-master chardet 4.0.0 (aiohttp-ch

TAHRI Ahmed R. 2 Nov 30, 2022
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

Sulaiman Haque 2 Nov 21, 2021
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
A pytest plugin to skip `@pytest.mark.slow` tests by default.

pytest-skip-slow A pytest plugin to skip @pytest.mark.slow tests by default. Include the slow tests with --slow. Installation $ pip install pytest-ski

Brian Okken 19 Jan 04, 2023
Green is a clean, colorful, fast python test runner.

Green -- A clean, colorful, fast python test runner. Features Clean - Low redundancy in output. Result statistics for each test is vertically aligned.

Nathan Stocks 756 Dec 22, 2022
Baseball Discord bot that can post up-to-date scores, lineups, and home runs.

Sunny Day Discord Bot Baseball Discord bot that can post up-to-date scores, lineups, and home runs. Uses webscraping techniques to scrape baseball dat

Benjamin Hammack 1 Jun 20, 2022
User-interest mock backend server implemnted using flask restful, and SQLAlchemy ORM confiugred with sqlite

Flask_Restful_SQLAlchemy_server User-interest mock backend server implemnted using flask restful, and SQLAlchemy ORM confiugred with sqlite. Backend b

Austin Weigel 1 Nov 17, 2022
Nokia SR OS automation

Nokia SR OS automation Nokia is one of the biggest vendors of the telecommunication equipment, which is very popular in the Service Provider segment.

Karneliuk.com 7 Jul 23, 2022
Set your Dynaconf environment to testing when running pytest

pytest-dynaconf Set your Dynaconf environment to testing when running pytest. Installation You can install "pytest-dynaconf" via pip from PyPI: $ pip

David Baumgold 3 Mar 11, 2022
Test scripts etc. for experimental rollup testing

rollup node experiments Test scripts etc. for experimental rollup testing. untested, work in progress python -m venv venv source venv/bin/activate #

Diederik Loerakker 14 Jan 25, 2022
A grab-bag of nifty pytest plugins

A goody-bag of nifty plugins for pytest OS Build Coverage Plugin Description Supported OS pytest-server-fixtures Extensible server-running framework w

Man Group 492 Jan 03, 2023
frwk_51pwn is an open-sourced remote vulnerability testing and proof-of-concept development framework

frwk_51pwn Legal Disclaimer Usage of frwk_51pwn for attacking targets without prior mutual consent is illegal. frwk_51pwn is for security testing purp

51pwn 4 Apr 24, 2022
Python 3 wrapper of Microsoft UIAutomation. Support UIAutomation for MFC, WindowsForm, WPF, Modern UI(Metro UI), Qt, IE, Firefox, Chrome ...

Python 3 wrapper of Microsoft UIAutomation. Support UIAutomation for MFC, WindowsForm, WPF, Modern UI(Metro UI), Qt, IE, Firefox, Chrome ...

yin kaisheng 1.6k Dec 29, 2022
Language-agnostic HTTP API Testing Tool

Dredd — HTTP API Testing Framework Dredd is a language-agnostic command-line tool for validating API description document against backend implementati

Apiary 4k Jan 05, 2023
A Proof of concept of a modern python CLI with click, pydantic, rich and anyio

httpcli This project is a proof of concept of a modern python networking cli which can be simple and easy to maintain using some of the best packages

Kevin Tewouda 17 Nov 15, 2022
Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.

WebTest This wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server. This provides

Pylons Project 325 Dec 30, 2022
pytest plugin for testing mypy types, stubs, and plugins

pytest plugin for testing mypy types, stubs, and plugins Installation This package is available on PyPI pip install pytest-mypy-plugins and conda-forg

TypedDjango 74 Dec 31, 2022