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

发布于 2024-11-29 08:18:02 字数 2543 浏览 2 评论 0原文

我不知道 F# Silverlight 4 单元测试有任何项目模板(我通过“添加项目”在线搜索模板),因此我使用标准 F# Silverlight 库项目来保存我的单元测试(它们只是指向主单元测试的文件链接)项目文件)。我正在使用 xUnit.net Contrib for Silverlight。所以一切都编译了,但我不知道如何运行测试! TestDriven.NET 适用于我的正常单元测试项目,但不适用于此 Silverlight 单元测试项目。我尝试使用普通的 xUnit GUI 运行程序,但这也不起作用。有什么想法吗?

更新

xUnit.net Contrib 人员建议使用 Statlight 作为独立的控制台运行程序。使用 Copy Local=true 将 System.Xml.Linq 添加到我的测试项目后(我的项目不需要它,但 Statlight 需要),我能够运行指向我的测试程序集的可执行文件,没有任何错误,只是无法找到任何错误测试。

我尝试了很多事情来尝试并让测试被发现。我为我的测试模块提供了明确的名称空间。我尝试了不带空格的测试方法名称。我尝试使用实例成员的类型而不是模块中的函数。我什至尝试了显式的 --MethodsToTest 标志。什么都不起作用。

我不确定这是否是 Statlight 或 xUnit.net Contrib 的问题。

请注意,我只是想测试一个可由 Silverlight 项目使用的普通库,实际上并没有使用任何 Silverlight 功能(事实上,我以前没有任何 Silverlight 经验!)。

更新 2

我能够通过 NuGet 将普通的 xunit 程序集添加到我的项目中(尽管不是为 Silverlight 构建的,但它“强制”将其添加到项目中),然后使用 Testdriven.NET 成功运行测试。但我不确定这算不算“作弊”。也许不是因为我的测试仍然针对我的库的 Silverlight 版本进行编译...?

更新 3

虽然我仍然没有找到“合法”的 xunit 解决方案,但我能够让 Statlight 运行一些 NUnit 测试。除此之外,它只识别对类的实例方法的测试,它不识别对模块的函数的测试(尽管它可以在正常项目中很好地处理这些测试)。真糟糕。 (我向 Statlight 提交了问题

更新 4

我想为我的问题找到了一个超级黑客但可行的解决方案,但仍在寻找更好的东西。我的所有测试都是作为模块中的函数编写的 Xunit 测试(指向非 Silverlight 测试项目的文件链接),但我无法让 Statlight 找到任何测试。但是,我能够让 Statlight 运行 NUnit 测试,这些测试以传统方式编写为类型的实例成员。因此,我能够启动以下单个 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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹梦 2024-12-06 08:18:02

我过去曾成功使用过 lighthouse ,但我不知道它是否适用于 xUnit 我用过它与MS测试。目前正在使用 AGUnit,它是基于 statlight 的 resharper 插件与 nunit 的结合,尽管它对 仅限 silverlight 项目——如果将其制作成 xap 文件,它们就可以工作。

I've used lighthouse sucessfully in the past, but I don't know if it works with xUnit I used it with 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文