Skip to main content

Rehearser makes writing reliable unit tests super easy!

Project description

Quick Start

You can use Rehearser to build reliable unit tests quickly


Development Flow:

graph LR

R["Rehearsal<br>runs"]--"interactions<br>file<br>case 1"-->A1["Adjust your<br>expectation"]-->C1["Create<br>Mocks"]-->U1("Unit Test 1")-->F["Finalize<br>Implementation"]
R["Rehearsal<br>runs"]--"interactions<br>file<br>case 2"-->A2["Adjust your<br>expectation"]-->C2["Create<br>Mocks"]-->U2("Unit Test 2")-->F["Finalize<br>Implementation"]
R["Rehearsal<br>runs"]--"interactions<br>file<br>case ..."-->A3["Adjust your<br>expectation"]-->C3["Create<br>Mocks"]-->U3("Unit Test ...")-->F["Finalize<br>Implementation"]
R["Rehearsal<br>runs"]--"interactions<br>file<br>case N"-->AN["Adjust your<br>expectation"]-->CN["Create<br>Mocks"]-->UN("Unit Test N")-->F["Finalize<br>Implementation"]

1. Install Rehearser:

pip install rehearser

2. Creating a Rehearser Proxy:

  • Component to be tested : Usage
  • External services: ProductService and UserService
graph LR

Usgae["Usage"] -- uses --> ProductService["ProductService"]-- uses -->C["Cache"]
Usgae["Usage"] -- uses --> UserService["UserService"]-- uses -->C["Cache"]
  • In this step, we create Rehearser Proxies for instances ProductService() and UserService(), 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


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.3.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

rehearser-0.1.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file rehearser-0.1.3.tar.gz.

File metadata

  • Download URL: rehearser-0.1.3.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for rehearser-0.1.3.tar.gz
Algorithm Hash digest
SHA256 65355eb5a7c1b6cd886b721ef6f07a5188118def65c1e4b842caa790c4ec3faf
MD5 b1c7e665f341db3e7a66b43cfea536c0
BLAKE2b-256 6e1cf4f49b590649cfbc2f35d67bd7e8f96e91d4f6c0bcb6df98b9d883e05ccd

See more details on using hashes here.

File details

Details for the file rehearser-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: rehearser-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for rehearser-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8868ef83ce67ea5c2bf4c1f8d1ce26f31c5fce5171f81f83be508a19f112780e
MD5 e65de5eb46cd645fc8ccdd57eff5ca6e
BLAKE2b-256 f56714abf65211ffa343b6f41d8c8be68a69106654fc40697dd2906a63509d84

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page