-
Notifications
You must be signed in to change notification settings - Fork 14
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.TestandServices.Test.GenerateValuesexist 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, andDataGenerators\*.dbl).
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.
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.
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.
-
Tutorial 2: Building a Service from Scratch
- Creating a Basic Solution
- Enabling OData Support
- Configuring Self Hosting
- Entity Collection Endpoints
- API Documentation
- Single Entity Endpoints
- OData Query Support
- Alternate Key Endpoints
- Expanding Relations
- Postman Tests
- Supporting CRUD Operations
- Adding a Primary Key Factory
- Adding Create Endpoints
- Adding Upsert Endpoints
- Adding Patch Endpoints
- Adding Delete Endpoints
-
Harmony Core CLI Tool
-
OData Aware Tools
-
Advanced Topics
- CLI Tool Customization
- Adapters
- API Versioning
- Authentication
- Authorization
- Collection Counts
- Customization File
- Custom Field Types
- Custom File Specs
- Custom Properties
- Customizing Generated Code
- Deploying to Linux
- Dynamic Call Protocol
- Environment Variables
- Field Security
- File I/O
- Improving AppSettings Processing
- Logging
- Optimistic Concurrency
- Multi-Tenancy
- Publishing in IIS
- Repeatable Unit Tests
- Stored Procedure Routing
- Suppressing OData Metadata
- Traditional Bridge
- Unit Testing
- EF Core Optimization
- Updating a Harmony Core Solution
- Updating to 3.1.90
- Creating a new Release
-
Background Information