Package: pytest-redis
pytest-redisWhat 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 usePlugin contains three fixturesredisdb- 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 redisSimply include one of these fixtures into your tests fixture list.#deftest_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()assertmy_functionality.did_somethingassertredisdb.get("did_it")==1For the example above works like following:pytest runs testsredis_proc starts redis database serverredisdb creates client connection to the servertest itself runs and finishesredisdb cleans up the redisredis_proc stops server (if that was the last test using it)pytest ends running testsYou can also create additional redis client and process fixtures if you’d need to:frompytest_redisimportfactoriesredis_my_proc=factories.redis_proc(port=None)redis_my=factories.redisdb('redis_my_proc')deftest_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()assertmy_functionality.did_somethingassertredis_my.get("did_it")==1NoteEach Redis process fixture can be configured in a different way than the others through the fixture factory arguments.Connecting to already existing redis databaseSome projects are using already running redis servers (ie on docker instances).
In order to connect to them, one would be using theredis_nooprocfixture.redis_external=factories.redisdb('redis_nooproc')deftest_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()assertmy_functionality.did_somethingassertredis_external.get("did_it")==1Standard configuration options apply to it.By default theredis_nooprocfixture would connect to Redis
instance using6379port attempting to make a successful socket
connection within15 seconds. The fixture will block your test run
within this timeout window. You can overwrite the timeout like so:# set the blocking wait to 5 secondsredis_external=factories.redis_noproc(timeout=5)deftest_redis(redis_external):"""Check that it's actually working on redis database."""redis_external.set('test1','test')# etc etcThese are the configuration options that are working on all levels with theredis_nooprocfixture:ConfigurationYou 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 argumentCommand line optionConfiguration option in your pytest.ini fileConfiguration optionsRedis server optionFixture factory argumentCommand line optionpytest.ini optionNoop process fixtureDefaultexecutableexecutable–redis-execredis_execLook in PATH for redis-server via shutil.whichhosthost–redis-hostredis_hosthost127.0.0.1portport–redis-portredis_portportrandomusernameusername–redis-usernameredis_usernameusernameNonepasswordpassword–redis-passwordredis_passwordpasswordNoneconnection timeouttimeout–redis-timeoutredis_timeout30number of databasesdb_count–redis-db-countredis_db_count8Whether to enable logging to the system loggersyslog–redis-syslogredis_syslogFalseRedis log verbosity levelloglevel–redis-loglevelredis_loglevelnoticeCompress dump filescompress–redis-compressredis_compressTrueAdd checksum to RDB fileschecksum–redis-rdbcompressredis_rdbchecksumFalseSave configurationsave–redis-saveredis_save“”Redis test instance data directory pathdatadir–redis-datadirredis_datadir“”Example usage:pass it as an argument in your own fixtureredis_proc=factories.redis_proc(port=8888)use--redis-portcommand line option when you run your testspy.test tests --redis-port=8888specify your port asredis_portin yourpytest.inifile.To do so, put a line like the following under the[pytest]section of yourpytest.ini:[pytest]redis_port=8888Options below are for configuring redis client fixture.Redis client optionFixture factory argumentCommand line optionpytest.ini optionDefaultdecode_responsedecode–redis-decoderedis_decodeFalseReleaseInstall pipenv and –dev dependencies first, Then run:pipenv run tbump [NEW_VERSION]