With database query steps, you can make calls to your database within the context of a mabl test. Interacting directly with your database is useful in a variety of scenarios, including…
- Validating that changes in the UI are appropriately reflected in the database
- Validating that changes in the database are appropriately reflected in the UI
- Populating mabl test data by storing data from your database as mabl variables
- Setting up data to simulate a specific state in your application (also known as data seeding) and tearing it down when the test is complete
Before creating database queries, you or someone on your team should be familiar with the schema of the database you're querying and know how to safely construct queries. Use caution when creating, deleting, or updating data.
This article explains how to add database query steps to your test, including how to…
Creating a database query
Before creating a database query, make sure you have configured a connection to the database in the configuration section of the mabl app: Configuration > Database connections.
All database query steps execute in the mabl cloud, regardless of whether they occur in the Trainer or during local or cloud execution. If you’re training a test that makes calls to an internal database that isn’t accessible from the public Internet, you must configure the training session to use an environment with a running Link Agent that has access to the database.
To create a database query step in the mabl Trainer, take the following actions:
- Click on the plus sign to add a new step.
- Select the Database query step.
- In the Query Editor, select a configured database.
- Click on the New query button.
- Write your query.
- If you want to include dynamically generated values, add parameters.
- If you're new to writing queries or want help writing a more complex query, try describing your query in the AI Query Generator.
- Click on the Run query button.
- Add an assertion that the query returns the expected results.
- If you want to use data from the result in subsequent test steps, create one or more variables.
- Select a save option:
- Save as one-time query: one-time queries are used once in the test and cannot be reused in other tests.
- Reusable query: reusable queries can be used more than once in the test and can be reused in other tests.
The Query Editor
You can also create reusable database queries from the queries page in the app: Tests > Queries.
Query parameters
With parameters, you can make your query more reusable in different contexts. To add an existing variable from your test as a parameter value, use mabl variable syntax. For example, a variable named test_var
would become {{@test_var}}
in the parameter section of the Query Editor.
In the query itself, most database types use the format :paramName
to reference a parameter. For database types that have a specific format that developers may be familiar with, the query editor supports that specific driver's format. For example, Microsoft SQL Server uses @paramName
format for parameters.
AI Query Generator
In addition to writing your own query, you can also create a query using generative AI. In the AI prompt input, describe the objective for your query using natural language and click Generate.
If you use the AI Query Generator, keep in mind the following:
- As with any generative AI capability, the results are not fully predictable, and errors can occur. Be sure to double-check any results before incorporating into your tests.
- Ensure the credentials that are used to set up the database connection include permissions to access the metadata table.
For more on generative AI at mabl, click here.
Assertions
Add assertions to confirm that the query returned the expected results.
Asserting on a result
To assert on a value in the results table, select the cell, open the Assertions tab, and click + Add assertion. Configure assertion details as needed.
Asserting on result metadata
To assert on result metadata, take the following steps:
- In the Assertions tab, click + Add assertion
- Update the Source to "Metadata"
- Select the Target: "Row count", "Column count", "Rows affected", or "Message"
- Configure the assertion type and value as needed.
Failure options
Database query steps fail if either the query fails or an assertion within the step fails. By default, database query steps are marked "Fail immediately", which means that if the step fails, the test stops running and is marked "failed".
If you want the test to continue running even if the step fails, update the behavior on failure in the Options tab of the Query Editor:
- "Fail at end of test" - if the step fails, the test continues running and is marked "failed" at the end.
- "Continue on failure & mark warning" - if the step fails, the step still passes and is marked with a warning.
Failure options for a database query step
Variables
Create variables from the results of a database query step. After saving the database query step, you can use the new variable just like any other variable in the mabl Trainer.
Creating variables from a result
To create a variable from a value in the results table, select the cell, open the Variables tab, and click + Add variable. Give your variable a name.
Creating variables from result metadata
To create a variable from result metadata, take the following steps:
- In the Variables tab, click + Add variable.
- Give the variable a name.
- Update the Source to "Metadata"
- Select the Target: "Row count", "Column count", "Rows affected", or "Message"
Storing a value from the result in a variable
Using an existing database query
To use an existing database query into your test, take the following steps:
- Click on the plus sign to add a new step.
- Select the Database query step.
- In the Query Editor, select a configured database.
- Select a query.
- Click Save to add the database query step to the test.
Updating a database query
To update a database query in the mabl Trainer, take the following steps:
- Hover over the database query step and click on the edit pencil.
- Click on the Edit query button.
- Update the database query as needed.
- Choose a save option:
- Save
- Save as new reusable query
- Save as one-time query
Saving updates to a reusable query in one test updates the query across all tests that use it.
You can also update reusable queries from the mabl app:
- Go to the queries page: Tests > Queries.
- Click on the query you'd like to update.
- Update the query as needed.
- Save the query.
When writing queries, keep in mind the following limitations:
- Query results cannot exceed 1MB in size.
- Query results cannot exceed 100 rows.
- Queries that return results from multiple tables are not supported (e.g.
SELECT * FROM TABLE1; SELECT * FROM TABLE2;
). If applicable, write the query usingJOIN
orUNION
instead. - Most database column types are supported, but some specialty column types are not supported, including JSON and Buffer types.
To avoid returning queries that exceed size limits, consider writing a more precise query so that you get only the necessary rows and columns. This type of precision querying is generally recommended as a best practice for constructing tests. Alternatively, if all the information is necessary, create multiple database steps.
If these limitations present challenges for your use case, please share feedback in the mabl Product Portal.