简体   繁体   English

如何使用 F# 在 NUnit 中打印?

[英]How to print in NUnit with F#?

I try to print something with the NUnit test framework in F#.我尝试使用 F# 中的 NUnit 测试框架打印一些东西。 It is not working when I run the piece of code below.当我运行下面的代码时它不起作用。 What is the problem?问题是什么?

I am using Visual Studio on an Mac if it matters.如果重要的话,我在 Mac 上使用 Visual Studio。

module firsttry_test

open NUnit.Framework

[<SetUp>]
let Setup() = ()

[<Test>]
let Test1() = Assert.Pass()

let __a= printf ("Hello Test here!");;

You may need to put your code into a class type as shown in the official Microsoft docs here .您可能需要将代码放入 class 类型中,如此处的官方 Microsoft 文档中所示

There are some issues with your code:您的代码存在一些问题:

  • you can use SetUp only with class types您只能将SetUp与 class 类型一起使用
  • put your code in a module, or a class with a default constructor (if you want to use SetUp)将您的代码放入一个模块中,或者一个带有默认构造函数的 class 中(如果您想使用 SetUp)
  • the let __a is global and not a function (there are no parens), so it only gets executed if something in the module gets accessed. let __a是全局的,而不是 function (没有括号),因此只有在模块中的某些内容被访问时才会执行。 Put it inside the test function and remove the let将其放入测试 function 并移除let

It's fine to use module level let bindings for tests, only if you want to use SetUp you need to put it in a class.可以使用模块级别的 let 绑定进行测试,只有当您想使用SetUp时,您才需要将其放入 class 中。

The following is fine:以下是好的:

module MyTests =
    [<Test>]
    let Test1() = 
        printf ("Hello Test here!")
        Assert.Pass()

Note that it depends on the test runner where you will see the console output.请注意,这取决于您将在哪里看到控制台 output 的测试运行程序。 For instance in VS a link is shown in the test output window, if you click it, you see the extra output.例如,在 VS 中,测试 output window 中显示了一个链接,如果单击它,您会看到额外的 output。 In NCrunch you'll see it directly in the result panel.在 NCrunch 中,您将直接在结果面板中看到它。 Other runners may behave differently.其他跑步者的行为可能会有所不同。

Some runners only show it when a test fails, in which case you'll have to access the raw output logs (Azure CI jobs that Microsoft uses behave like this).一些跑步者只在测试失败时显示它,在这种情况下,您必须访问原始 output 日志(Microsoft 使用的 Azure CI 作业的行为类似于此)。

Unfortunately, this does not work on Visual Studio for Mac.不幸的是,这不适用于 Visual Studio for Mac。 For getting output you need a failing test:要获得 output 你需要一个失败的测试:

[<Test>]
let Test1() = 

   printfn "Hello Test here!"
   Assert.Fail()

Now you'll see the message in the Test Results Output Window.现在您将在测试结果 Output Window 中看到消息。 Sometimes is not enabled, but just click on it and you'll see it).有时未启用,但只需单击它,您就会看到它)。

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

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