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 plugin for testing the idempotency of a function.

pytest-idempotent Pytest plugin for testing the idempotency of a function. Usage pip install pytest-idempotent Documentation Suppose we had the follo

Tyler Yep 3 Dec 14, 2022
Test utility for validating OpenAPI documentation

DRF OpenAPI Tester This is a test utility to validate DRF Test Responses against OpenAPI 2 and 3 schema. It has built-in support for: OpenAPI 2/3 yaml

snok 103 Dec 21, 2022
This repository contains a testing script for nmigen-boards that tries to build blinky for all the platforms provided by nmigen-boards.

Introduction This repository contains a testing script for nmigen-boards that tries to build blinky for all the platforms provided by nmigen-boards.

S.J.R. van Schaik 4 Jul 23, 2022
create custom test databases that are populated with fake data

About Generate fake but valid data filled databases for test purposes using most popular patterns(AFAIK). Current support is sqlite, mysql, postgresql

Emir Ozer 2.2k Jan 04, 2023
show python coverage information directly in emacs

show python coverage information directly in emacs

wouter bolsterlee 30 Oct 26, 2022
A command-line tool and Python library and Pytest plugin for automated testing of RESTful APIs, with a simple, concise and flexible YAML-based syntax

1.0 Release See here for details about breaking changes with the upcoming 1.0 release: https://github.com/taverntesting/tavern/issues/495 Easier API t

909 Dec 15, 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
Aplikasi otomasi klik di situs popcat.click menggunakan Python dan Selenium

popthe-popcat Aplikasi Otomasi Klik di situs popcat.click. aplikasi ini akan secara otomatis melakukan click pada kucing viral itu, sehingga anda tida

cndrw_ 2 Oct 07, 2022
DUCKSPLOIT - Windows Hacking FrameWork using Reverse Shell

Ducksploit Install Ducksploit Hacker setup raspberry pico Download https://githu

2 Jan 31, 2022
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
The (Python-based) mining software required for the Game Boy mining project.

ntgbtminer - Game Boy edition This is a version of ntgbtminer that works with the Game Boy bitcoin miner. ntgbtminer ntgbtminer is a no thrills getblo

Ghidra Ninja 31 Nov 04, 2022
Active Directory Penetration Testing methods with simulations

AD penetration Testing Project By Ruben Enkaoua - GL4Di4T0R Based on the TCM PEH course (Heath Adams) Index 1 - Setting Up the Lab Intallation of a Wi

GL4DI4T0R 3 Aug 12, 2021
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
Browser reload with uvicorn

uvicorn-browser This project is inspired by autoreload. Installation pip install uvicorn-browser Usage Run uvicorn-browser --help to see all options.

Marcelo Trylesinski 64 Dec 17, 2022
0hh1 solver for the web (selenium) and also for mobile (adb)

0hh1 - Solver Aims to solve the '0hh1 puzzle' for all the sizes (4x4, 6x6, 8x8, 10x10 12x12). for both the web version (using selenium) and on android

Adwaith Rajesh 1 Nov 05, 2021
Find index entries in $INDEX_ALLOCATION attributes

INDXRipper Find index entries in $INDEX_ALLOCATION attributes Timeline created using mactime.pl on the combined output of INDXRipper and fls. See: sle

32 Nov 05, 2022
Main purpose of this project is to provide the service to automate the API testing process

PPTester project Main purpose of this project is to provide the service to automate the API testing process. In order to deploy this service use you s

4 Dec 16, 2021
A tool to auto generate the basic mocks and asserts for faster unit testing

Mock Generator A tool to generate the basic mocks and asserts for faster unit testing. 🎉 New: you can now use pytest-mock-generator, for more fluid p

31 Dec 24, 2022
输入Google Hacking语句,自动调用Chrome浏览器爬取结果

Google-Hacking-Crawler 该脚本可输入Google Hacking语句,自动调用Chrome浏览器爬取结果 环境配置 python -m pip install -r requirements.txt 下载Chrome浏览器

Jarcis 4 Jun 21, 2022
Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

Leon 3.5k Dec 30, 2022