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
A simple serverless create api test repository. Please Ignore.

serverless-create-api-test A simple serverless create api test repository. Please Ignore. Things to remember: Setup workflow Change Name in workflow e

Sarvesh Bhatnagar 1 Jan 18, 2022
buX Course Enrollment Automation

buX automation BRACU - buX course enrollment automation Features: Automatically enroll into multiple courses at a time. Find courses just entering cou

Mohammad Shakib 1 Oct 06, 2022
A simple script to login into twitter using Selenium in python.

Quick Talk A simple script to login into twitter using Selenium in python. I was looking for a way to login into twitter using Selenium in python. Sin

Lzy-slh 4 Nov 20, 2022
Pytest support for asyncio.

pytest-asyncio: pytest support for asyncio pytest-asyncio is an Apache2 licensed library, written in Python, for testing asyncio code with pytest. asy

pytest-dev 1.1k Jan 02, 2023
Aioresponses is a helper for mock/fake web requests in python aiohttp package.

aioresponses Aioresponses is a helper to mock/fake web requests in python aiohttp package. For requests module there are a lot of packages that help u

402 Jan 06, 2023
Fi - A simple Python 3.9+ command-line application for managing Fidelity portfolios

fi fi is a simple Python 3.9+ command-line application for managing Fidelity por

Darik Harter 2 Feb 26, 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
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
Auto-hms-action - Automation of NU Health Management System

🦾 Automation of NU Health Management System 🤖 長崎大学 健康管理システムの自動化 🏯 Usage / 使い方

k5-mot 3 Mar 04, 2022
Sixpack is a language-agnostic a/b-testing framework

Sixpack Sixpack is a framework to enable A/B testing across multiple programming languages. It does this by exposing a simple API for client libraries

1.7k Dec 24, 2022
Generate random test credit card numbers for testing, validation and/or verification purposes.

Generate random test credit card numbers for testing, validation and/or verification purposes.

Dark Hunter 141 5 Nov 14, 2022
Python tools for penetration testing

pyTools_PT python tools for penetration testing Please don't use these tool for illegal purposes. These tools is meant for penetration testing for leg

Gourab 1 Dec 01, 2021
Flexible test automation for Python

Nox - Flexible test automation for Python nox is a command-line tool that automates testing in multiple Python environments, similar to tox. Unlike to

Stargirl Flowers 941 Jan 03, 2023
Automated tests for OKAY websites in Python (Selenium) - user friendly version

Okay Selenium Testy Aplikace určená k testování produkčních webů společnosti OKAY s.r.o. Závislosti K běhu aplikace je potřeba mít v počítači nainstal

Viktor Bem 0 Oct 01, 2022
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 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
Python Testing Crawler 🐍 🩺 🕷️ A crawler for automated functional testing of a web application

Python Testing Crawler 🐍 🩺 🕷️ A crawler for automated functional testing of a web application Crawling a server-side-rendered web application is a

70 Aug 07, 2022
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
1st Solution to QQ Browser 2021 AIAC Track 2

1st Solution to QQ Browser 2021 AIAC Track 2 This repository is the winning solution to QQ Browser 2021 AI Algorithm Competition Track 2 Automated Hyp

DAIR Lab 24 Sep 10, 2022
Code for "SUGAR: Subgraph Neural Network with Reinforcement Pooling and Self-Supervised Mutual Information Mechanism"

SUGAR Code for "SUGAR: Subgraph Neural Network with Reinforcement Pooling and Self-Supervised Mutual Information Mechanism" Overview train.py: the cor

41 Nov 08, 2022