Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding unit tests to a F# project in VSCode

I'm using VSCode and the Ionide suite of packages to create a console application in F#. I need to add unit tests to the application so that when I ctrl+shift+p FAKE: Build the project, the tests are run during the build process.

I've created a dummy project in Github as an example.

Initially, the test dir was not there. I created the test dir and into that folder created a second project TestProj.Test (in hindsight, I should have used more descriptive names) for testing purposes. I added the .fsproj file from TestProj to this project so that I could reference the SimpleFunctions.fs. NUnit.Framework and FsUnit are added to the TestProj.Test. Test.fs contains two simple tests.

I intentionally created the TestProj.Test as an F# library because I read on SO that the testing project needed to be a library rather than a console app.

I added lines 9, 31-37, and 47 to the default build.fsx file that comes from Ionide.. However, when I build the whole project (i.e., TestProj), the build fails and I get the following error:

  1) System.Exception: NUnit: cannot run tests (the assembly list is empty).
   at Fake.NUnitSequential.NUnit(FSharpFunc`2 setParams, IEnumerable`1 assemblies) in C:\code\fake\src\app\FakeLib\UnitTest\NUnit\Sequential.fs:line 22
   at [email protected](Unit _arg3)
   at Fake.TargetHelper.runSingleTarget(TargetTemplate`1 target) in C:\code\fake\src\app\FakeLib\TargetHelper.fs:line 492

Line 22 of the Sequential.fs suggests that assemblies is empty.

What am I doing wrong? How should I set up the build.fsx file so that the tests in TestProj.test run successfully? Alternatively, is there something wrong with the Tests.fs file in TestProj.Test? This seems particularly difficult; is there an easier way to include tests that run automatically with VSCode, Iondide, and F#?

like image 962
Steven Avatar asked Oct 29 '22 17:10

Steven


1 Answers

There are a few issues in your project:

  • trying to test before build "Clean" ==> "Test" ==> "Build" ==> "Deploy"
    => change target dependencies to "Clean" ==> "Build" ==> "Test" ==> "Deploy"

  • separate paket configuration for test (paket.dependencies, paket.lock in test subfolder) which leads to inconsistent versions of referenced dependencies
    => remove paket.dependencies and paket.lock from test

  • poisonous mix of NUnit versions
    => remove explicit references to NUnit.Framework from paket.dependencies and run paket.exe install

  • invalid type extension in test project
    => change to type Test() or delete useless file

  • building creates output of all projects (and not just src/app) in ./build but tests look for DLLs in ./test
    => change test file pattern to buildDir + "**/*.Test.dll"

  • if you want to use NUnit3
    => open Fake.Testing and use NUnit3 instead of NUnit

finally, you should commit paket.bootstrapper.exe

I recommend you either use a predefined template or start small and make sure you understand each step and check that it is working as expected. Once you've run over the point of a non-working solution it is extremely hard to get back on track.

like image 139
CaringDev Avatar answered Nov 15 '22 12:11

CaringDev