Best practices
Best practices for writing tests in Momentic
Write clear and concise tests
A great AI-powered test should be easy to understand, reading almost like a tech spec. At the same time, it should comprehensively cover your application’s surface area. Here are some tips to achieve this:
- Single responsibility: Each test should focus on verifying a single behavior or functionality. This way, tests are easy to maintain and provide a very specific signal when they fail. Avoid testing multiple aspects of an application in one test case.
- Avoid complex logic: Keep complex logic within the test to a minimum. If many interactive steps are needed to setup the correct state, consider creating a Module so that the logic can be reused across multiple tests.
- Meaningful assertions: Craft assertions that match how a user would gauge if the application is working as expected. Avoid assertions that are either too vague or too specific. Ensure that you have enough assertions to catch incorrect behavior at all key page transitions.
Make sure to always follow best practices for describing elements and writing assertions.
Follow the AAA pattern
The Arrange-Act-Assert (AAA) pattern is a common structure for writing tests. It ensures that tests are organized and easy to follow.
- Arrange: Set up the necessary pre-conditions and inputs for the test.
- Act: Perform the action that should be tested.
- Assert: Verify that the outcome matches the expected result.
In practice, this pattern might look like:
Keep tests independent
Independent tests should be capable of running in any order without affecting each other. This makes the test suite more reliable and easier to debug.
- No shared state: Avoid sharing state between tests. Each test should set up its own state and clean up afterwards.
- Reset state: Ensure that any state modified during a test is reset after the test completes.
Setup and teardown
If a test requires specific state to be set up, consider using a setup step at the beginning of the test to establish the necessary conditions.
For test cleanup, you can use teardown steps at the end of the test to reset any state that was modified during the test.
A common pattern is to expose private authenticated APIs for the test environment. This allows tests to set up and tear down state as needed without exposing these APIs to the public.
Create modules to share logic
When you notice that steps are often duplicated in your tests, consider grouping them into a module to share logic. This can help reduce duplication and make tests easier to maintain.
For example, if you have a username and log in password that are used in multiple tests, you can create a module that provides these values.
This module can then be included in other tests as needed.
Parameterized modules
You can also create parameterized modules that accept arguments to customize their behavior. This can be useful when you need to test similar scenarios with different inputs.
When editing a module, you can add parameters to the module definition and use them in the module steps using the {{ }}
syntax. Reference module parameters using the following syntax {{ env.PARAMETER_NAME }}
.
For more information, see Test context.
Leverage the CLI
The CLI enables teams to follow a standard developer workflow and offers the following features over Momentic Cloud:
- Seamlessly collaborate through your team’s existing source control system
- Test in local and private environments (including CI/CD clusters) that are not accessible from Momentic Cloud
- Verify and deliver testing changes at the same time as application code changes
Click here for details on how to get started with the CLI.
Was this page helpful?