Skip to content

Repeatable Unit Tests

mattl91 edited this page Mar 9, 2026 · 12 revisions

Harmony Core Logo

Repeatable Unit Tests

Successful repeatable unit tests require data that is the same for each run, as well as test constants (key values) that are used for each run. Solutions created from the harmonycore template (see Tutorial 5: Unit Testing Generated OData Services) include functionality that makes it easy to create a unit test project and to set up data and constants for repeatable unit testing.

Before using the functionality discussed in this topic, complete Tutorial 5: Unit Testing Generated OData Services so that

  • Services.Test and Services.Test.GenerateValues exist in your solution.
  • unit test generation is enabled (Enable unit tests = Yes) and code has been regenerated.
  • generated unit test files exist (for example UnitTestEnvironment.dbl, TestConstants.Properties.dbl, and DataGenerators\*.dbl).

Using "Enable test file creation"

With a solution created from the harmonycore template, and after the unit test projects/code described in Tutorial 5: Unit Testing Generated OData Services are in place, the easiest way to keep unit tests repeatable is to instruct Harmony Core to rebuild data before each unit test run. You can do this by setting the Enable test file creation option on the OData tab of the CLI Tool. In the default setup (when file logicals are not defined and the Disable file logicals option in the CLI Tool is not enabled), this recreates data files in the directory specified by the Data Folder option on the Solution tab in the CLI Tool.

For ISAM files, this option requires both a .txt source file and a matching .xdl file.

Restoring Valid Data

As part of your build, you can copy test-ready ISAM files to the folder specified by the Data Folder option on the Solution tab in the CLI Tool. (When you create a solution from the harmonycore template--for example via dotnet new harmonycore -o MyApi--the default data folder SampleData is created.) Because Data Folder is used for a hierarchical search that starts from the test assembly output path (then parent folders), it is best to create a folder with the specified name in the Services.Test folder and then copy test-ready data to that folder with each build. If the data folder is in Services.Test, your web service will not use that data for production operations.

If you have already set file logicals in your environment, Harmony Core will use those existing logical translations instead of overriding them to Data Folder. Also note that if Disable file logicals is enabled, Harmony Core will not remap file logicals to Data Folder.

Establishing Test Constants

Along with stable data, repeatable unit tests require stable test constants (key values). Unit test generation creates the TestConstants property model (for example TestConstants.Properties.dbl), and the Services.Test.GenerateValues project reads your current data and writes values to Services.Test\TestConstants.Values.json.

You can generate or refresh these constants by running the Services.Test.GenerateValues project from the solution root after SolutionDir has been set to the solution root.

dotnet run --project Services.Test.GenerateValues\Services.Test.GenerateValues.synproj

This command reads your current data and writes updated values to TestConstants.Values.json.

If you regenerate test data (for example, by using Enable test file creation), run the command above again before executing dotnet test. This keeps constants aligned with the data used by the tests and preserves repeatability.

Clone this wiki locally