-
Notifications
You must be signed in to change notification settings - Fork 14
Unit Testing
Before using the following procedure to create unit tests, you must first use the harmonycore project template to create a solution and you must generate code for your API. The harmonycore template includes functionality that automates unit test creation. (It also includes functionality for repeatable unit tests; see Repeatable Unit Tests for more information.)
To create and run unit tests for a solution created from the harmonycore project template, do the following:
-
In Visual Studio, open the solution for your Harmony Core web service.
-
Add a Synergy .NET Core Console application project. To do this, you can right-click the solution node in Solution Explorer, select Add > New Project from the context menu, select Synergy DBL as the language, and then select the "Console APP (.NET Core)" template. Note that by default the code generation process depends on this project being named Services.Test. If you give this project a different name, you must set the regen.bat option TestProject to that name. For example:
set TestProject=Services.MyUnitTests
-
Open project properties for the new project (e.g., right-click the project node in Solution Explorer and select Properties from the context menu), select the Common Properties tab, and select the “Use common properties” option. Then save the changes.
-
Uncomment the following line in regen.bat (by removing the
rem
keyword from the beginning of the line), and then save the file:set ENABLE_UNIT_TEST_GENERATION=YES
-
Delete Program.dbl from your unit test project. The next step will generate a mainline program (and many other files) for the project.
-
Run regen.bat. See Code Generation for more information on running this file. This generates a number of files and subfolders for unit testing and puts them in the folder for your unit test project. (This does not add these files to the project. You’ll do this in the next step.)
-
Add generated files, including those in the DataGenerators, Models, and UnitTests subfolders, to the new project. For example, right-click the project node in Solution Explorer, select Add > Existing Item from the context menu, and add all the generated .dbl files to the project by selecting them and clicking Add.
-
Add the following NuGet package references to the new project. Use the latest versions of these packages that support ASP.NET Core 2.2. For information on adding these references, see Microsoft documentation (e.g., Install and manage packages in Visual Studio…).
IdentityModel Microsoft.AspNetCore.Mvc Microsoft.AspNetCore.Mvc.Core Microsoft.AspNetCore.Mvc.Testing (install version 2.2.0) Microsoft.AspNetCore.OData (install version 7.2.1) Microsoft.AspNetCore.SignalR.Client Microsoft.AspNetCore.StaticFiles Microsoft.EndityFrameworkCore Microsoft.Extensions.Logging.Console Microsoft.NET.Test.Sdk Microsoft.NETCore.App MSTest.TestAdaptor MSTest.TestFramework Newtonsoft.Json Nito.AsyncEx Synergex.SynergyDE.Build Synergex.SynergyDE.synrnt System.Linq.Dynamic.Core System.Text.Encoding.CodePages
-
In the new project, add references to the Repository and Services projects in the solution.
-
Set the new project as the startup project (e.g., right-click the node for the new project in Solution Explorer, and select "Set as Startup Project" from the context menu).
-
Build the solution in Visual Studio. If there are errors, make sure your unit test project has the correct NuGet packages (step 8) and that you've added references to the Repository and Services projects (step 9).
-
Update the TestConstants.Values.dbl file with reasonable test values. Note that although this is a generated file, CodeGen will not overwrite it once you’ve made a change to it.
-
Open a Command Prompt window and set the SolutionDir environment variable to the directory for your solution. Be sure to include a final backslash. For example:
set SolutionDir=c:\myHcSolution\
-
At the command prompt, navigate to the directory for your unit test project and use the
dotnet test
command to run the test from the command line. For example:cd Services.Test dotnet test
As tests are run, test output is written to the console. When tests are complete, you'll see a message like the following:
Total tests: 59`
Passed: 45`
Failed: 14`
Total time: 45.3551 Seconds`
Once you have set up your unit tests, you can use the ENABLE_CREATE_TEST_FILES option in regen.bat to rebuild test data with each test run. See Repeatable Unit Tests for more information.
-
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 Code Generator
-
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