Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing tests for script files in F#

Tags:

tdd

f#

xunit

I have some functionality withing a script file, setup.fsx I would like to test those. xUnit and the likes needs the functions to be tested to be part of the assembly.

So I am thinking of renaming my script from setup.fsx to setup.fs extension, then loading it from another script file. But then my script depends on

#r "System.Xml"
#r "System.Xml.Linq"

which I would then have to specify in the calling script (away from where the dependency actually arises)

Are there anyway to integrate tests based on scripts in the xUnit worflow ? What organization is suggested for writing tests for script files ?

(may be we need a visual studio extension for tests in scripts and not in assembly..)

like image 382
nicolas Avatar asked Nov 04 '22 20:11

nicolas


1 Answers

Even if you just add fsx script to Visual Studio, you can still compile setup.fsx into a normal project together with other (possibly fs) files, so you should be able to keep the script as a normal script file in Visual Studio and, at the same time, reference it from a project or from a command line tool that builds your tests.

I tried doing that with the following test.fsx file:

module Demo
#r "System.Xml.Linq.dll"
open System.Xml.Linq

let test () = 
    let d = XDocument(XElement(XName.Get("foo")))
    d.ToString()

You definitely need some module Name declaration at the beginning (so that you can access functions from other files), but otherwise it can be any fsx file. The other file that I used was test.fs:

module Main
open Demo    
test() |> printfn "%A"

This is just for testing, but here you could write your unit tests. If you compile the files with the following command, you get standard assembly that you could pass to xUnit (note, the compiler can pick the #r tag from test.fsx, we do not have to write the reference explicitly):

fsc.exe --target:library test.fsx test.fs

I think you could get the same configuration in Visual Studio if you add a library project and then manually add link to the file (which can point to a file elsewhere in your solution structure) using something like this in the fsproj file:

<Compile Include="..\Eslewhere\In\Your\Project\Tree\File.fsx">
  <Link>File.fsx</Link>
</Compile>

Note that when you add fsx file using "Add Item", it is marked as "Include" but not as "Compile", so it does not get compiled as part of the project. The above should include it in the project and it should tell the compiler to include it in the compiled assembly too.

Warning: that said, I think it might be better to test just compiled dll files using standard unit tests. If you want to test fsx files, I would just add a couple of lines as tests at the end and run them by hand (select, Alt+Enter). The reason is that fsx files are supposed to be changed quite often and so having too solid testing may limit your flexibility. On the other hand, once code gets more solid, it makes sense to move it to a dll file.

like image 159
Tomas Petricek Avatar answered Nov 10 '22 21:11

Tomas Petricek