Python wrapper & utils for the Mountebank over the wire test double tool.
Project description
mbtest
Opinionated Python wrapper & utils for the Mountebank over the wire test double tool.
Includes pytest fixture and PyHamcrest matchers.
Setup
Install with pip:
pip install mbtest
(As usual, use of a venv or virtualenv is recommended.) Also requires Mountebank to have been installed:
npm install mountebank@2.9 --omit=dev
(Alternatively, you can attach to an instance of Mountebank running elsewhere, perhaps in docker.)
Basic example
import requests
from hamcrest import assert_that
from brunns.matchers.response import is_response
from mbtest.matchers import had_request
from mbtest.imposters import Imposter, Predicate, Response, Stub
def test_request_to_mock_server(mock_server):
# Set up mock server with required behavior
imposter = Imposter(Stub(Predicate(path="/test"),
Response(body="sausages")))
with mock_server(imposter):
# Make request to mock server - exercise code under test here
response = requests.get(f"{imposter.url}/test")
assert_that("We got the expected response",
response, is_response().with_status_code(200).and_body("sausages"))
assert_that("The mock server recorded the request",
imposter, had_request().with_path("/test").and_method("GET"))
Needs a pytest fixture, most easily defined in conftest.py
:
import pytest
from mbtest import server
@pytest.fixture(scope="session")
def mock_server(request):
return server.mock_server(request)
This will take care of starting and stopping the Mountebank server for you. Examples of more complex predicates can be found in the integration tests.
See the Documentation for more.
Contributing
Requires make and tox. PyEnv may also come in handy so tests can be run against various Python versions.
Integration tests run against an instance of Mountebank running in Docker.
Currently, the Makefile
targets use Python 3.8 and 3.11 via tox, so a quick-start setup could be:
brew install pyenv colima docker
versions=(3.8 3.12)
for v in "${versions[@]}"; do pyenv install $v -s; done
pyenv local 3.8 3.12
pip install tox tox-pyenv
colima start
In order to run make test
, you'll also need to have Mountebank installed locally:
npm install mountebank@2.9 --production
(mbtest
is tested against Mountebank versions back as far as 1.16, but obviously only features supported by the Mountebank version you're using will work.)
After that, you should be ready to roll; running make test
will let you know if your setup is correct.
Running make precommit
tells you if you're OK to commit. For more options, run:
make help
Releasing
Requires gh, setuptools,
wheel and twine. To release version n.n.n
, first
update the version number in setup.py
, then:
version="n.n.n" # Needs to match new version number in setup.py.
git checkout -b "release-$version"
make precommit && git commit -am"Release $version" && git push --set-upstream origin "release-$version" # If not already all pushed, which it should be.
gh release create "v$version" --target "release-$version" --generate-notes
python setup.py sdist bdist_wheel && twine upload dist/*$version*
git checkout master && git merge "release-$version"
git push
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mbtest-2.13.1.tar.gz
.
File metadata
- Download URL: mbtest-2.13.1.tar.gz
- Upload date:
- Size: 22.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb386037ee93aedcdc88e3cf287f851f47701f79d9a3e661f2d9647880013892 |
|
MD5 | 463fa7b7eb99131e391b00b5fbbae4b1 |
|
BLAKE2b-256 | dd888fd4d5af308b8c3fcf7c46580182500f0659606733eaf0886ea46864cd45 |
File details
Details for the file mbtest-2.13.1-py3-none-any.whl
.
File metadata
- Download URL: mbtest-2.13.1-py3-none-any.whl
- Upload date:
- Size: 24.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29e0c3c525eacd830249e89d55281e26450eb02d9a922b40f48aa3e0e82d546b |
|
MD5 | 10842e6663826c80f640b5c837814f2d |
|
BLAKE2b-256 | 231d363a9ff9727d0db0ce336693311fc92dda337dba23305984ef60510005a9 |