Rehearser makes writing reliable unit tests super easy!
Project description
Quick Start
You can use Rehearser
to build reliable unit tests quickly
Development Flow:
["Rehearsal run"] --> ["Adjust interactions files"] --> ["Create Mocks"] --> ["Write Unit Tests"] --> ["Finalize Implementation"]
1. Install Rehearser:
pip install rehearser
2. Creating a Rehearser Proxy:
- Component to be tested :
Usage
- External services:
ProductService
,UserService
andCache
["Usage"] --uses--> ["ProductService"], ["UserService"] --uses--> ["Cache"]
- In this step, we create Rehearser Proxies for instances
ProductService()
andUserService()
, respectively.
rp_product = RehearserProxy(ProductService())
rp_user = RehearserProxy(UserService())
3. Generate Interactions:
Generate mock objects using the interactions created in the previous step.
# Apply patches to UserService and ProductService
with patch(
"rehearser_examples.examples.example1.usage.UserService",
return_value=rp_user,
), patch(
"rehearser_examples.examples.example1.usage.ProductService",
return_value=rp_product,
):
# Rehearsal run
Usage().run_example()
# Generate interactions files
rp_user.set_interactions_file_directory("./raw_files/rehearser_proxy/")
rp_user.write_interactions_to_file()
rp_product.set_interactions_file_directory("./raw_files/rehearser_proxy/")
rp_product.write_interactions_to_file()
- Notes: The interaction files are in json format, and you can adjust these thru editor manually before using these for further Mock object generation.
4. Write Unit Test:
These will be your unit test body:
# Instantiate mock objects
mock_users = MockGenerator(
interactions_src="./raw_files/rehearser_proxy/UserService/latest_interactions.json"
).create_mock()
mock_products = MockGenerator(
interactions_src="./raw_files/rehearser_proxy/ProductService/latest_interactions.json"
).create_mock()
# Apply patches to UserService and ProductService
with patch(
"rehearser_examples.examples.example1.usage.UserService",
return_value=mock_users,
), patch(
"rehearser_examples.examples.example1.usage.ProductService",
return_value=mock_products,
):
# Instantiate Usage with the mocked services
result = Usage().run_example()
# Insert your test assertions here
self.assertTrue(result, "run_example() failed")
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
rehearser-0.1.5.tar.gz
(9.9 kB
view details)
Built Distribution
rehearser-0.1.5-py3-none-any.whl
(10.8 kB
view details)
File details
Details for the file rehearser-0.1.5.tar.gz
.
File metadata
- Download URL: rehearser-0.1.5.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f753439623e1da33f0f356f608e7e8927f0784eddc913dcbdafc0d6f47dec3b0 |
|
MD5 | 0a9ac1c6dff5376e301739fa70c60979 |
|
BLAKE2b-256 | 5fdb3e36e51e7a8398b82eb58d1a043406d7b0daafbb0d217a1b19e1b67930dc |
File details
Details for the file rehearser-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: rehearser-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a1e9d3241d5791bd120b9682fb6b1b4522112006d953b84e2e000a8f8f50d16 |
|
MD5 | 7d83319adec8ccf0d1485a1c3f650d56 |
|
BLAKE2b-256 | ac8f8e3eb8a117e6228ed5eb18ef92c400e7e59871d919dccc46bbb4882ec73f |