Rehearser makes writing reliable unit tests and contract tests super easy!
Project description
🎭 Rehearser - We make writing reliable unit tests and contract tests super easy! 🎭
Rehearser is a robust and intuitive tool designed to save much of your time in unit and contract testing creation. With its unique approach to replaying interactions, Rehearser ensures your software components interact seamlessly and error-free.
Key Features:
- Easy to use 🎭
- Replay Interactions for python method, HTTP or etc... 🔄
- User-Friendly Interface 🖥️
- Integration with Popular Testing Frameworks 🤖
- Support for Multiple Programming Languages 🐍🌐☕
- Open Source ❤️
- Community Support 👫
Join the Rehearser community and make your testing process as smooth as a rehearsal! 🎭
#UnitTesting #ContractTesting #TestingTools #OpenSource #DeveloperTools #Rehearser
Tutorial
This is the code we wanted to test:
def run_example(self):
"""
This is the code that we want to test.
"""
# Create users
self.user_service.add_user(1, "User 1")
self.user_service.add_user(2, "User 2")
self.user_service.add_user(3, "User 3")
# Create products
self.product_service.add_product(100, "Product 100")
self.product_service.add_product(200, "Product 200")
self.product_service.add_product(300, "Product 300")
# List users
print("Users:")
for user_id in range(1, 4):
user_data = self.user_service.get_user(user_id)
print(f"User ID: {user_id}, User Data: {user_data}")
# List products
print("Products:")
for product_id in range(100, 400, 100):
product_data = self.product_service.get_product(product_id)
print(f"Product ID: {product_id}, Product Data: {product_data}")
return True
Installation:
pip install rehearser
Creating a Rehearser Proxy:
from rehearser import RehearserProxy
from examples.example1.usage import ProductService, UserService
product_service = ProductService()
user_service = UserService()
rp_product = RehearserProxy(product_service)
rp_user = RehearserProxy(user_service)
In this example, rp_product
and rp_user
serve as proxies for product_service
and user_service
, respectively.
Generate Interactions:
The following code shows how to generate mock objects using the interaction files created in the previous step.
@patch(
"rehearser.examples.example1.usage.UserService",
return_value=RehearserProxy(UserService(Cache())),
)
@patch(
"rehearser.examples.example1.usage.ProductService",
return_value=RehearserProxy(ProductService(Cache())),
)
def test_rehearser_run_example(
self, rp_product: Any, rp_user: Any
) -> None:
# Rehearsal run
result = 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()
Test with Generated Mock using Interaction Files:
Run your unit test patched with mocks now.
# 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.example1.usage.UserService",
return_value=mock_users,
), patch(
"rehearser.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
Built Distribution
File details
Details for the file rehearser-0.1.1.tar.gz
.
File metadata
- Download URL: rehearser-0.1.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d856e247f47821f4e2e56b294875bd02cf2d05d13ee11b96a398e27d78cb399b |
|
MD5 | 046cf3a065f6a929d14c0bb03560cefd |
|
BLAKE2b-256 | 2cfc334927ffe9d5b39ac2c86c16b9fd243e6386212727078895bdf50255d075 |
File details
Details for the file rehearser-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: rehearser-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.3 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 | bb38b7769c07787e9583a9cf322628b1b08427d58793793f0bd92117f0db392d |
|
MD5 | de48560f2423e7693ee1a0333e514cb9 |
|
BLAKE2b-256 | 53cd618528b5bd74749017f5e358ba33d56031a5c9cea15066993bb833bef858 |