locked
Trying to get VS 2010 Test Tools working with F# project RRS feed

  • Question

  • I’ve found several internet posts on this topic, but no answers as to how to get this working. Most responses are along the lines of “get xUnit.net”. I’m new to integrating unit tests in VS, I have a C# project I did not write that works fine, and it seems like the built in test tools and runner are more full-featured than xUnit’s tools, which are not integrated into VS, although the set-up in code of xUnit tests seems less intrusive. I have successfully created a xUnit test in F# and run it in the xUnit GUI runner. (VS2010 SP1)
    I’ve added the right Test Tools reference and intellisync works on the Test Tool attributes, so everything appears in order, but Run > Tests in Current Context results in “No tests were run because no tests are loaded or the selected tests are disabled”.
    open Microsoft.VisualStudio.TestTools.UnitTesting
    [<TestClass>]
    type TestCaseUtil = class
        new() = {}  
        [<TestMethod>]
        [<Description("my description")>]
        member this.myTest() =
            Assert.AreEqual(myTree, x)    //value assignments not shown in this example
    end
    Monday, July 4, 2011 7:00 PM

Answers

  • By accident I stumbled upon http://www.atrevido.net/blog/CategoryView,category,FSharp.aspx It's kind of annoying you have to do this kind of hack for a "1st class" CLR language.

    Quoting the entire short article:

     

    In Visual Studio 2010, you can now get your F# unit tests loaded by the IDE. First, create your F# test project. This is just a normal library referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework and having test classes and so on. The only change you need to do is go into the configuration and set the output folder to "bin" for both debug and release (instead of bin\debug).

    Next, create a C# Test Project, and delete the code file. Then add an existing item, and navigate to the bin directory of your test project output. Select the DLL and Add as Link.

    Finally, right click the solution, and change the project dependencies so the C# test project depends on your F# test project.

    That's it. Now the IDE will pickup your F# tests and allow you to manage, run, and debug them right from VS. You may have to restart the IDE after setting all this up for it to work smoothly. 

    • Marked as answer by jackfoxyMVP Wednesday, July 13, 2011 9:33 PM
    Monday, July 4, 2011 9:49 PM
  • As you've noticed, unfortunately F# does not play nicely with MSTest out of the box. Some other potential options are mentioned here: http://stackoverflow.com/questions/1468772/how-to-handle-unit-tests-in-f


    If you'd like to echo your support for the issue on Connect it does inform our future decision making process http://connect.microsoft.com/VisualStudio/feedback/details/564586/make-using-mstest-more-accessible-to-f-projects

    Wednesday, July 6, 2011 1:42 AM

All replies

  • By accident I stumbled upon http://www.atrevido.net/blog/CategoryView,category,FSharp.aspx It's kind of annoying you have to do this kind of hack for a "1st class" CLR language.

    Quoting the entire short article:

     

    In Visual Studio 2010, you can now get your F# unit tests loaded by the IDE. First, create your F# test project. This is just a normal library referencing Microsoft.VisualStudio.QualityTools.UnitTestFramework and having test classes and so on. The only change you need to do is go into the configuration and set the output folder to "bin" for both debug and release (instead of bin\debug).

    Next, create a C# Test Project, and delete the code file. Then add an existing item, and navigate to the bin directory of your test project output. Select the DLL and Add as Link.

    Finally, right click the solution, and change the project dependencies so the C# test project depends on your F# test project.

    That's it. Now the IDE will pickup your F# tests and allow you to manage, run, and debug them right from VS. You may have to restart the IDE after setting all this up for it to work smoothly. 

    • Marked as answer by jackfoxyMVP Wednesday, July 13, 2011 9:33 PM
    Monday, July 4, 2011 9:49 PM
  • Ok I can see why you want your Unit-Tests for F# in F#.

    But I tend to use the pragmatic approach: writing the unit-tests in C#. It has the added benifits for checking the public-facing code for those nasty F#ish smells (like FSharpOption, FastFunc, etc.) that just tend to spoil the "interop"-experience I'm trying to achive.

    Tuesday, July 5, 2011 9:57 AM
  • As you've noticed, unfortunately F# does not play nicely with MSTest out of the box. Some other potential options are mentioned here: http://stackoverflow.com/questions/1468772/how-to-handle-unit-tests-in-f


    If you'd like to echo your support for the issue on Connect it does inform our future decision making process http://connect.microsoft.com/VisualStudio/feedback/details/564586/make-using-mstest-more-accessible-to-f-projects

    Wednesday, July 6, 2011 1:42 AM
  • Thanks! It is good just to get clarity that something does not work. I'll try the C# hack soon.
    Wednesday, July 6, 2011 2:02 AM
  • Hi JackFox,

    I think I have noticed when opening the F# source code project (on CodePlex) that they've built their test as part of F# script files without using any testing framework and they run those script files as a post-build command.

    Have a look and try to replicate.

    Friday, July 8, 2011 6:00 PM
  • Hi David,
    You mean the source at this page http://fsharppowerpack.codeplex.com/SourceControl/list/changesets?
    I just downloaded and unzipped the latest (Nov2010) and the unit tests are using nunit.framework, not MSTest.
    Sunday, July 10, 2011 12:04 AM
  • After a little fumbling I got the hack described at http://www.atrevido.net/blog/CategoryView,category,FSharp.aspx  to work!
    Wednesday, July 13, 2011 9:34 PM
  • Hi JackFox,

    I stumbled across this hack as well, but as yet I haven't been able to get it to work. I'm expecting to be able to view my test methods in a Test view and select them for execution a la C# unit testing. I don't see any tests listed in the Test View.

    I have a solution containing my F# test target project, an F# Test project containing the following simple class: (adapted from your example above)

    open Microsoft.VisualStudio.TestTools.UnitTesting
    [<TestClass>]
    type TestCaseUtil = class
        new() = {}  
        
        [<TestMethod>]
        [<Description("my description")>]
        member this.myTest() =
            Assert.AreEqual(1, 1)
    end

    and finally there is the C# Test project which SHOULD, as far as I understand, act as a proxy for the real F# test project.

    Any ideas what I'm doing wrong?

    Thanks
    Kevin

    Thursday, August 25, 2011 10:39 AM
  • Thanks JackFox for the question and follow up. Nice work! I've upvoted most of the posts here.

    I decided not to bother with F# and MSTest in VS 2010, and have been working quite happily with NUnit, writing the tests in F# and using Unquote for assertions. It all works nicely - especially being able to develop tests informally in fsi and then transferring to the test library.

    I see that there is a project to integrate MSTest with F# in Visual Studio 11. 

    http://visualstudiogallery.msdn.microsoft.com/51ebe64a-899b-4959-8c24-b0148ed6b264

    I'll have a look at this it would be nice to keep all the development and testing within F# and VS.

    Wednesday, March 21, 2012 2:48 AM
  • Now we have the official test support for unit test.

    I just published the F# unit test template to enable using F# for the unit testing:

    http://visualstudiogallery.msdn.microsoft.com/432eb82c-345e-4502-be56-015fe051a210

    Friday, March 23, 2012 11:55 PM