简体   繁体   English

如何运行包含基于 xUnit.net Contrib 的单元测试的 F# Silverlight 库项目?

[英]How to run F# Silverlight Library project holding xUnit.net Contrib based unit tests?

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 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项目文件)。 I am using xUnit.net Contrib for Silverlight .我正在为 Silverlight 使用 xUnit.net Contrib 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. TestDriven.NET 适用于我的正常单元测试项目,但不适用于此 Silverlight 单元测试项目。 I tried to use the normal xUnit GUI runner but that doesn't work either?我尝试使用普通的 xUnit GUI 运行器,但这也不起作用? Any ideas?有任何想法吗?

Update更新

The xUnit.net Contrib folks recommended using Statlight as a standalone console runner. xUnit.net Contrib 的人建议使用Statlight作为独立的控制台运行程序。 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.在使用 Copy Local=true 将 System.Xml.Linq 添加到我的测试项目中之后(我的项目不需要它,但 Statlight 需要它),我能够运行指向我的测试程序集的可执行文件而没有任何错误,除非它找不到任何错误测试。

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.我什至尝试了明确的--MethodsToTest标志。 Nothing works.没有任何效果。

I'm not sure if this would be a problem with Statlight or xUnit.net Contrib.我不确定这是否是 Statlight 或 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.).请注意,我只是想测试一个可以被 Silverlight 项目使用的普通库,实际上并没有使用任何 Silverlight 功能(实际上,我之前没有任何 Silverlight 经验。)。

Update 2更新 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.我能够通过 NuGet 将正常的 xunit 程序集添加到我的项目中(尽管不是为 Silverlight 构建的,但它“强制”它进入),然后使用 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...?也许不是因为我的测试仍然是针对我的库的 Silverlight 版本编译的......?

Update 3更新 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.虽然我仍然没有使用“合法”的 xunit 解决方案,但我能够让 Statlight 运行一些 NUnit 测试。 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).除了,它只识别对 class 的实例方法的测试,它不识别对模块功能的测试(虽然它可以在正常项目中处理这些很好)。 Bummer.真可惜。 (I filed an issue with Statlight) (我向 Statlight 提出了问题

Update 4更新 4

I've figured out a super hacky but workable solution to my problem, still looking for something better though.我想出了一个超级hacky但可行的解决方案来解决我的问题,但仍在寻找更好的东西。 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.我所有的测试都是 Xunit 测试(指向非 Silverlight 测试项目的文件链接)编写为模块中的函数,但我无法让 Statlight 找到任何测试。 However, I was able to get Statlight to run NUnit tests which are written in the traditional fashion as instance members of a type.但是,我能够让 Statlight 运行 NUnit 测试,这些测试以传统方式编写为类型的实例成员。 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):因此,我能够启动以下单个 NUnit 测试,该测试在程序集中找到所有 Xunit 测试并调用它们(所以我一次只能看到一个失败的测试,但除此之外它运行良好):

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

I've used lighthouse sucessfully in the past, but I don't know if it works with xUnit I used it with mstest.我过去成功地使用过灯塔,但我不知道它是否适用于 xUnit 我将它与 mstest 一起使用。 Currently working with AGUnit which is statlight based resharper plugin combined with nunit, though it has difficulty with dll only silverlight projects--they work if you make it into a xap file.目前正在使用 AGUnit,它是基于 statlight 的 resharper 插件与 nunit 相结合,尽管它很难使用dll 仅 silverlight项目——如果你将它制作成 xap 文件,它们就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM