Pytest-Prosper¶
Helper libraries for test coverage and general maintenance of services. Making test coverage easier across Prosper projects!
Quickstart¶
setup(
...
tests_require=[
'pytest-prosper',
]
)
Pytest-Prosper is suggested as a tests_require
install. Though there are some general use utilities, this library is not meant for production use.
Index¶
Getting Started¶
ProsperTestUtils is a simple Python library compatable with >=3.5.
This collection is designed to help:
- Test case development
- Production environment helpers
Most application should use this library as a tests_require
rather than directly pip install prospertestutils
# setup.py
setup(
...
tests_require=[
'pytest',
'prospertestutils',
...
],
)
Plugin Development¶
Functionality is separated by family. Utilities should be built with an easy-to-use API and split into easy-to-test units.
Documentation¶
pip install .[dev]
sphinx-apidoc -f -o docs/source prosper/test_utils
sphinx-build -b html docs/ webpage/
schema_utils¶
Many Prosper projects rely on NoSQL sources and backends. In an effort to signal humans only when there are breaking changes, ProsperTestUtils implements an auto-jsonschema system for tracking data sources and their changes over time.
prosper.test_utils.schema_helper()
understands how to manage minor updates (keys added), and will raise exceptions if bigger changes occur.
schema_helper¶
schema_helper(
data,
data_source,
schema_name,
schema_group,
config,
)
Powered by genson, raw JSON data goes in, and a `JSONschema`_ is tested against a MongoDB. Minor updates and new sources will be handled automatically, but larger changes will raise exceptions. Also major changes will dump to an update file.
update-prosper-schemas¶
git clone git@github.com:EVEprosper/ProsperTestUtils.git
cd ProsperTestUtils
pip install -e .
python setup.py test
update-prosper-schemas --dump-config > my_config.cfg
## Update SECRETS in .cfg file ##
update-prosper-schemas prosper-schema-update_2018-05-14T01/44/17.440168.json --verbose --config=my_config.cfg
When major updates are required, a human needs to push updates to the database. Just run the test suite and then run update-prosper-schemas
on the dumped .json file.
Pytest Fixtures¶
Half of the power of PyTest is its Fixtures. pytest-prosper
comes with some baked-in features that every Prosper project should find useful.
Secret Management¶
Secret management is challenging, but prosper.common.prosper_cli has a built in --secret-cfg
and these fixtures mimic that api.
pytest.ini
[pytest]
config_path = path/to/app.cfg
python setup.py test --secret-cfg=secrets.ini
import pytest
def test_something_secret(secret_cfg):
handle = login(
username=secret_cfg.get_option('credentials', 'username'),
password=secret_cfg.get_option('credentials', 'password'),
)
Though the app.cfg
should be in the repository, secrets.ini
should not be. By providing a secrets.ini
file at test time, secrets can remain unpaired and safe.
API Reference¶
test_utils¶
test_utils package¶
Submodules¶
test_utils.config_utils module¶
test_utils.docker_utils module¶
common test utilities for Prosper projects
-
test_utils.docker_utils.
assert_docker
(xfail=False, force_retry=False, **kwargs)¶ validates docker connection
Parameters: - xfail (bool) – change behavior from rasie->xfail
- force_retry (bool) – force recheck docker status
- kwargs – docker.from_env() handles
- Raises
- pytest.xfail: soft-fail for expected failure exceptions.DockerNotFound: no docker connection
test_utils.exceptions module¶
exceptions for test_utils cases
-
exception
test_utils.exceptions.
DockerNotFound
¶ Bases:
test_utils.exceptions.DockerUtilsException
cannot find/connect to Docker in environment
-
exception
test_utils.exceptions.
DockerUtilsException
¶ Bases:
test_utils.exceptions.TestUtilsException
general exception for docker_utils libraries
-
exception
test_utils.exceptions.
FirstRunWarning
¶ Bases:
UserWarning
unable to find existing schema in database
-
exception
test_utils.exceptions.
MajorSchemaUpdate
¶ Bases:
test_utils.exceptions.SchemaUtilsException
protect database from major schema updates – require human to run update
-
exception
test_utils.exceptions.
SchemaUtilsException
¶ Bases:
test_utils.exceptions.TestUtilsException
general exception for schema_utils libraries
-
exception
test_utils.exceptions.
TestUtilsException
¶ Bases:
Exception
general exception for prosper.test_utils modules
-
exception
test_utils.exceptions.
TestUtilsWarning
¶ Bases:
Warning
general warning for prosper.test_utils modules
-
exception
test_utils.exceptions.
UnhandledDiff
¶ Bases:
test_utils.exceptions.SchemaUtilsException
DeepDiff had output, but not handled as change
test_utils.schema_utils module¶
schema testers
-
class
test_utils.schema_utils.
MongoContextManager
(config, _testmode=False, _testmode_filepath=PosixPath('/home/docs/checkouts/readthedocs.org/user_builds/pytest-prosper/checkouts/latest/prosper/test_utils'))¶ Bases:
object
context manager for mongo connections
Notes
connection_str requires {username}, {password} format strings
Parameters: - config (
prosper.common.prosper_config.ProsperConfig
) – configparser-like object - _testmode (bool) – use a localdb rather than a prod one
- _testmode_filepath (str) – path to localdb
- config (
-
class
test_utils.schema_utils.
Update
¶ Bases:
enum.Enum
enum for classifying what kind of update is required
-
first_run
= 'first_run'¶
-
major
= 'major'¶
-
minor
= 'minor'¶
-
no_update
= 'no_update'¶
-
-
test_utils.schema_utils.
schema_helper
(data, data_source, schema_name, schema_group, config, _collection_name='prosper_schemas', _testmode=False, _dump_filepath='')¶ - test helper: generates schemas from data and checks them against a mongoDB.
- Updates for minor changes (adding keys) Raises errors for major changes
Parameters: - data (dict) – data to generate jsonschema from (raw data)
- data_source (str) – link to source
- schema_name (str) – name of resource for tracking
- schema_group (str) – group (project name) for grouping
- config (
prosper.common.ProsperConfig
) – config object with [MONGO] credentials - _testmode (bool) – run on local database with TinyMongo
- _dump_filepath (str) – path to dump files to
Returns: ???