2

I am unaware of any project templates for F# Silverlight 4 unit tests (I searched Online for templates through Add Project), so I am using the standard F# Silverlight Library project to hold my unit tests (which are just file links to the main unit test project files). I am using xUnit.net Contrib for Silverlight. So everything compiles but I don't know how to run the tests! TestDriven.NET works for my normal unit test project but not this Silverlight unit test project. I tried to use the normal xUnit GUI runner but that doesn't work either. Any ideas?

Update

The xUnit.net Contrib folks recommended using Statlight as a standalone console runner. After adding System.Xml.Linq to my test project with Copy Local=true (my project doesn't need it but Statlight does) I was able to run the executable pointing at my test assembly without any errors except it was unable to find any tests.

I tried lots of things to try and allow the tests to be found. I gave explicit namespaces to my test modules. I tried test method names without spaces. I tried types with instance members instead of functions in modules. I even tried the explicit --MethodsToTest flag. Nothing works.

I'm not sure if this would be a problem with Statlight or xUnit.net Contrib.

Please note that I am merely trying to test a plain library which can be consumed by Silverlight projects and doesn't actually use any Silverlight features (indeed, I don't have any prior Silverlight experience!).

Update 2

I was able to add the normal xunit assembly to my project through NuGet (which "forces" it in despite not being built for Silverlight) and then run the tests successfully using Testdriven.NET. But I'm not sure if this is "cheating". Perhaps not because my tests are still compiled against the Silverlight version of my library...?

Update 3

While I still haven't had any luck with a "legit" xunit solution, I was able to get Statlight to run some NUnit tests. Except, it only recognizes tests on instance methods of a class, it doesn't recognize tests on functions of a module (while it can handle these just fine in a normal project). Bummer. (I filed an issue with Statlight)

Update 4

I've figured out a super hacky but workable solution to my problem, still looking for something better though. All of my tests are Xunit tests (file links to the non-Silverlight test project) written as functions in modules but I can't get Statlight to find any of the tests. However, I was able to get Statlight to run NUnit tests which are written in the traditional fashion as instance members of a type. Thus I was able to whip up the following single NUnit test which finds all the Xunit tests in the assembly and invokes them (so I can only see one failing test as a time, but other than that it does work well):

namespace SilverlightTesting

open Xunit
open NUnit.Framework

[<TestFixture>]
type TestRunner() =
    let isFact (attr:obj) =
        match attr with
        | :? Xunit.FactAttribute as attr when attr.Skip = null -> true
        | _ -> false

    [<Test>]
    member this.Run () =
        let assm = System.Reflection.Assembly.GetExecutingAssembly()
        let tys = assm.GetTypes()
        let mutable count = 0
        for ty in tys do
            let methods = ty.GetMethods()
            for mi in methods do
                let attrs = mi.GetCustomAttributes(false)
                if attrs |> Array.exists isFact then
                    printf "running test `%s`..." mi.Name
                    mi.Invoke(null,null) |> ignore
                    printfn "passed"
                    count <- count + 1
        printfn "All %i tests passed." count
4

1 回答 1

1

我过去成功地使用过灯塔,但我不知道它是否适用于 xUnit 我将它与 mstest 一起使用。目前正在使用 AGUnit,它是基于 statlight 的 resharper 插件与 nunit 相结合,尽管它对仅使用 dll 的 silverlight项目有困难——如果你将它制作成 xap 文件,它们就可以工作。

于 2011-08-14T04:19:48.330 回答