How to: Generate tests

Kolo’s test generation always starts with a traced request. If you don’t yet have a trace, read How to: Trace Django Requests.

  1. For your traced request, navigate from the “Trace” page to the “Test” page.

image
  1. You’ll see a page similar to the screenshot above, with your integration test waiting for you 🥳

  2. Save the test file to your desired location, or copy/paste it manually.

  3. Run the test in the same way you normally would to check that it’s working. Make any edits as desired.

Customizing test generation

Every codebase has different testing requirements and code style. You can customize Kolo’s test generation to suit your needs. Learn more about how to do this in How to: Customize test generation

How does test generation work?

Based on the data captured in the trace, Kolo first generates a test plan which includes the various “steps” it intends to include in the integration test. Each step in the plan is derived from some information in the trace itself. Then, based on the plan, Kolo generates the test code on the left of the page. Learn more about how Kolo test generation works in this blog post

Generating tests with the Kolo CLI

You can also generate tests using the Kolo CLI.

Basic example:

kolo generate-test trc_abc123 > core/test.py

You can also pass multiple trace_ids to include multiple requests in one test:

kolo generate-test trc_abc123 trc_def456 > core/test.py

By default, Kolo will generate pytest-style tests. If you prefer unittest-style tests, pass --unittest:

kolo generate-test --unittest trc_abc123 > core/test.py

Additionally you can specify custom test names using --test-name. This also works for test classes in unittest: --test-class

For more configuration options take a look at How to: Customize test generation and Kolo CLI

Where to find trace_ids

You can find the trace_ids for your traces in the web app or VSCode extension (via the ... or right click menus). Alternatively you can list recent traces and their trace_ids using kolo trace list

Known limitations

  • Kolo autogenerates test fixtures, but in some cases there isn’t enough data to generate them accurately. In that case, you will need to either add additional fixtures manually or tweak the ones we generate.

  • Time machine or freezegun is used to make sure dealing with time is easier and more consistent. If you’re not using one of these, you will need to install one or define a step hook to use your preferred library. For more information about step and plan hooks read How to: Customize test generation