Metadata-Version: 2.1
Name: rehearser
Version: 0.1.1
Summary: Rehearser makes writing reliable unit tests and contract tests super easy!
Home-page: https://github.com/kevinchwong/rehearser
Author: Kevin C. Wong
Author-email: kevinchwong@gmail.com
Keywords: unit testing,contract testing,testing tools,open source,developer tools,rehearsal,proxy,rehearser
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pytest
Requires-Dist: redis
Requires-Dist: types-redis

# 🎭 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:
```python
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**:

```bash
pip install rehearser
```

## **Creating a Rehearser Proxy**: 

```python
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.
```python
@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.
```python
# 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")
```
