5

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 FSI_0005.Build.clo@31-3.Invoke(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#?

Steven
  • 3,238
  • 21
  • 50
  • Your 'Test' target is looking for 'NUnit.Test.*.dll' whereas 'TestProj.Test' produces a like-named dll. BTW, there are a few other possible improvements to your project structure. If you want, I'd raise a PR by EOD WEST. – CaringDev Oct 27 '16 at 05:37
  • 1
    For setting up a full-fledged F# project including documentation you could have a look at [ProjectScaffold](https://fsprojects.github.io/ProjectScaffold/) – CaringDev Oct 27 '16 at 05:46
  • I've used ProjectScaffold before but there's so much stuff included that I don't need or understand. I was hoping to avoid that but may resort to having to use it if I can't get a testing framework up and running on my own. Also, maybe it's too early in my day, but I don't understand the abbreviations "PR," "EOD," and "WEST" as you've used them here. Explanation please? – Steven Oct 27 '16 at 12:28
  • Sorry, for that wall of abbreviations :-) PR = *P*ull *R*equest for your repository, EOD = End Of Day, WEST = West European Summer Time. Did the answer help though? – CaringDev Oct 27 '16 at 14:29
  • Your comment helped minimally but that's because I don't understand how all of the parts fit together! Also yes, a PR would be helpful! – Steven Oct 27 '16 at 15:11
  • created [PR](https://github.com/stevenranney/TestProj/pull/1) as promised – CaringDev Oct 28 '16 at 00:23

1 Answers1

2

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.

CaringDev
  • 8,391
  • 1
  • 24
  • 43