Skip to content

Unit Testing

Steve Ives edited this page Apr 27, 2020 · 2 revisions

Harmony Core Logo

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:

  1. In Visual Studio, open the solution for your Harmony Core web service.

  2. 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
    
  3. 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.

  4. 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
    
  5. Delete Program.dbl from your unit test project. The next step will generate a mainline program (and many other files) for the project.

  6. 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.)

  7. 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.

  8. 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
    
  9. In the new project, add references to the Repository and Services projects in the solution.

  10. 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).

  11. 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).

  12. 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.

  13. 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\
    
  14. 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.

Clone this wiki locally