Test your integration with test clocks
Overview
Test clocks make it easier to test your Billing integration and make sure it behaves as designed. When you use test clocks you simulate the forward movement of time in test mode, which causes Billing resources, like Subscriptions, to change state and trigger webhook events. This means that, for example, you don’t have to wait a year to see how your integration handles a payment failure for a quarterly or annual renewal.
Here are some other things you can do with test clocks:
- Test complex simulations such as upgrading or changing plans mid-cycle.
- Ensure your integration processes Billing lifecycle webhooks correctly.
- Validate that your app handles trials correctly.
- Build and test multi-phase subscription schedules.
How to use test clocks
The typical cycle for test clocks consists of creating a simulation, setting up the test case and advancing the time of the simulation, then reviewing how your integration handled those changes.
Follow these steps to start using test clocks:
- Create a test clock
- Set up your testing simulation
- Advance the clock’s time
- Monitor and handle the changes
- Update the simulation
- Delete the clock
You can advance the clock’s time, monitor changes, and update the simulation as often as you need to test different cases.
Create a clock and set its time
To start a simulation, create a clock and set its frozen time. The temporal starting point for all subscriptions associated with this clock. You can set this to a time in the future or in the past to test different simulations, but you can only move it forward in time after you set it.
Set up your simulation
Next, set up the test case for your simulation. You need to create a customer first, then a subscription for them.
Both the customer and subscription objects are associated with the clock object you created in the first step. In the Dashboard, the
icon indicates that an object is associated with a clock.Advance the simulated time
After you’ve created the test clock and set up your test case, advance the simulated time of the clock. The first time you do this, you’ll advance the time from the initial frozen time you set at the creation of the clock. As you advance time, you can see how your integration works when subscriptions end, renew, or undergo other changes (like upgrading from a free trial to a paid subscription).
You can advance test clocks by any increment, but you can only advance them 2 intervals at a time from their initial frozen time. The length of the interval is based on the shortest subscription interval associated with the test clock, which is determined by the recurring price. For example, if you have a monthly subscription, you can only advance the clock up to 2 months at a time. If the test clock has no subscriptions or subscription schedules, you can advance it up to 2 years from the initial frozen time.
Monitor and handle changes
After a successful API request or Dashboard operation, the clock takes a few seconds to advance to the specified time. To know when the clock has changed state, you can use webhooks to listen for event notifications or you can poll the clock. The Dashboard also reflects the changes. For example, you can go to the invoices page to check whether an invoice was created or paid for your subscription.
If you use webhooks, listen to the following event notifications. Before production, make sure your integration correctly handles the other billing-specific event notifications in addition to the ones listed below.
Event | Description |
---|---|
test_helpers.test_clock.advancing | The clock has started to advance but hasn’t reached the specified time. |
test_helpers.test_clock.ready | The clock has completed advancing to the specified time. |
To poll the state of the clock, retrieve it by ID to examine its status
.
Update the simulation
You can continue to make changes to your simulation and advance the clock for simulations like:
- Adding a customer balance.
- Making a mid-cycle upgrade.
- Adding one-off invoice items.
After each update, monitor the changes again. Repeat as many times as you need to satisfy your test case.
Delete the clock
Test clocks are automatically deleted 30 days after you create them, but you can delete them when you’re done testing to ensure a clean test environment.
Deleting the clock also deletes the test customers associated with the clock and cancels their subscriptions. Test clocks are only available in test mode, so you can’t delete any production objects when you delete a clock.