|
| 1 | +# How do I test the Server? |
| 2 | + |
| 3 | +Testing your Server code in a SAFE app is just the same as in any other dotnet app, and you can use the same tools and frameworks that you are familiar with. These include all of the usual suspects such as [NUnit](https://nunit.org/), [XUnit](https://xunit.net/), [FSUnit](https://fsprojects.github.io/FsUnit/), [Expecto](https://github.com/haf/expecto), [FSCheck](https://fscheck.github.io/FsCheck/), [AutoFixture](https://github.com/AutoFixture/AutoFixture) etc. |
| 4 | + |
| 5 | +In this guide we will look at using **Expecto**, as this is included with the standard SAFE template. |
| 6 | + |
| 7 | +## **I'm using the standard template** |
| 8 | + |
| 9 | +### Using the Expecto runner |
| 10 | + |
| 11 | +If you are using the standard template, then there is nothing more you need to do in order to start testing your Server code. |
| 12 | + |
| 13 | +In the tests/Server folder, there is a project named `Server.Tests` with a single script demonstrating how to use Expecto to test the TODO sample. |
| 14 | + |
| 15 | +In order to run the tests, instead of starting your application using |
| 16 | +```powershell |
| 17 | +dotnet run |
| 18 | +``` |
| 19 | + |
| 20 | +you should instead use |
| 21 | +```powershell |
| 22 | +dotnet run RunTests |
| 23 | +``` |
| 24 | +This will execute the tests and print the results into the console window. |
| 25 | + |
| 26 | +<img src="../../../img/expecto-results.png"/> |
| 27 | + |
| 28 | +> This method builds and runs the Client test project too, which can be slow. If you want to run the Server tests alone, you can simply navigate to the tests/Server directory and run the project using `dotnet run`. |
| 29 | +
|
| 30 | +### Using dotnet test or the Visual Studio Test runner |
| 31 | + |
| 32 | +If you would like to use dotnet tests from the command line or the test runner that comes with Visual Studio, there are a couple of extra steps to follow. |
| 33 | + |
| 34 | +#### 1. Install the Test Adapters |
| 35 | + |
| 36 | +Run the following commands at the root of your solution: |
| 37 | +```powershell |
| 38 | +dotnet paket add Microsoft.NET.Test.Sdk -p Server.Tests |
| 39 | +``` |
| 40 | +```powershell |
| 41 | +dotnet paket add YoloDev.Expecto.TestSdk -p Server.Tests |
| 42 | +``` |
| 43 | + |
| 44 | +#### 2. Disable EntryPoint generation |
| 45 | + |
| 46 | +Open your ServerTests.fsproj file and add the following element: |
| 47 | + |
| 48 | +```xml |
| 49 | +<PropertyGroup> |
| 50 | + <GenerateProgramFile>false</GenerateProgramFile> |
| 51 | +</PropertyGroup> |
| 52 | +``` |
| 53 | + |
| 54 | +#### 3. Discover tests |
| 55 | + |
| 56 | +To allow your tests to be discovered, you will need to decorate them with a `[<Tests>]` attribute. |
| 57 | + |
| 58 | +The provided test would look like this: |
| 59 | +```fsharp |
| 60 | +[<Tests>] |
| 61 | +let server = testList "Server" [ |
| 62 | + testCase "Adding valid Todo" <| fun _ -> |
| 63 | + let storage = Storage() |
| 64 | + let validTodo = Todo.create "TODO" |
| 65 | + let expectedResult = Ok () |
| 66 | +
|
| 67 | + let result = storage.AddTodo validTodo |
| 68 | +
|
| 69 | + Expect.equal result expectedResult "Result should be ok" |
| 70 | + Expect.contains (storage.GetTodos()) validTodo "Storage should contain new todo" |
| 71 | +] |
| 72 | +``` |
| 73 | + |
| 74 | +#### 4. Run tests |
| 75 | + |
| 76 | +There are now two ways to run these tests. |
| 77 | + |
| 78 | +From the command line, you can just run |
| 79 | +```powershell |
| 80 | +dotnet test tests/Server |
| 81 | +``` |
| 82 | +from the root of your solution. |
| 83 | + |
| 84 | +Alternatively, if you are using Visual Studio or VS Mac you can make use of the built-in test explorers. |
| 85 | + |
| 86 | +<img src="../../../img/test-runner.png" style="height: 200px;"/> |
| 87 | + |
| 88 | +## **I'm using the minimal template** |
| 89 | + |
| 90 | +If you are using the minimal template, you will need to first configure a test project as none are included. |
| 91 | + |
| 92 | +#### 1. Add a test project |
| 93 | + |
| 94 | +Create a `.Net` console project called `Server.Tests` in the tests/Server folder. |
| 95 | + |
| 96 | +```powershell |
| 97 | +dotnet new console -lang F# -n Server.Tests -o tests/Server |
| 98 | +dotnet sln add tests/Server |
| 99 | +``` |
| 100 | + |
| 101 | +#### 2. Reference the Server project |
| 102 | + |
| 103 | +Reference the Server project from the Server.Tests project: |
| 104 | + |
| 105 | +```powershell |
| 106 | +dotnet add tests/Server reference src/Server |
| 107 | +``` |
| 108 | + |
| 109 | +#### 3. Add Expecto to the Test project |
| 110 | + |
| 111 | +Run the following command: |
| 112 | + |
| 113 | +```powershell |
| 114 | +dotnet add tests/Server package Expecto |
| 115 | +``` |
| 116 | + |
| 117 | +#### 4. Add something to test |
| 118 | + |
| 119 | +Update the Server.fs file in the Server project to extract the message logic from the router like so: |
| 120 | +```fsharp |
| 121 | +let getMessage () = "Hello from SAFE!" |
| 122 | +
|
| 123 | +let webApp = |
| 124 | + router { |
| 125 | + get Route.hello (getMessage () |> json ) |
| 126 | + } |
| 127 | +``` |
| 128 | + |
| 129 | +#### 5. Add a test |
| 130 | + |
| 131 | +Replace the contents of `tests/Server/Program.fs` with the following: |
| 132 | + |
| 133 | +``` fsharp |
| 134 | +open Expecto |
| 135 | +
|
| 136 | +let server = testList "Server" [ |
| 137 | + testCase "Message returned correctly" <| fun _ -> |
| 138 | + let expectedResult = "Hello from SAFE!" |
| 139 | + let result = Server.getMessage() |
| 140 | + Expect.equal result expectedResult "Result should be ok" |
| 141 | +] |
| 142 | +
|
| 143 | +[<EntryPoint>] |
| 144 | +let main _ = runTestsWithCLIArgs [] [||] server |
| 145 | +``` |
| 146 | + |
| 147 | +#### 6. Run the test |
| 148 | + |
| 149 | +```powershell |
| 150 | +dotnet run -p tests/Server |
| 151 | +``` |
| 152 | + |
| 153 | +This will print out the results in the console window |
| 154 | + |
| 155 | +<img src="../../../img/expecto-results.png"/> |
| 156 | + |
| 157 | +#### 7. Using dotnet test or the Visual Studio Test Explorer |
| 158 | + |
| 159 | +Add the libraries `Microsoft.NET.Test.Sdk` and `YoloDev.Expecto.TestSdk` to your Test project, using NuGet. |
| 160 | + |
| 161 | + |
| 162 | +> The way you do this will depend on whether you are using NuGet directly or via Paket. See [this recipe](../package-management/add-nuget-package-to-server.md) for more details. |
| 163 | +
|
| 164 | +You can now add `[<Test>]` attributes to your tests so that they can be discovered, and then run them using the dotnet tooling in the same way as explained earlier for the standard template. |
0 commit comments