我在使用dotnet new xunit
创建的项目中进行了以下XUnit测试:
type Scenarios(output : ITestOutputHelper) =
[<Fact>]
member __.``Output shows up`` () =
output.WriteLine("I'm here")
此方法seems to have worked before,但无论我使用dotnet test
还是dotnet xunit
,运行测试都不会显示任何输出:
> dotnet test
Build started, please wait...
Build completed.
Test run for C:\Work\OSS\Streamstone.fs\tests\StreamstoneFs.Tests\bin\Debug\netcoreapp2.0\StreamstoneFs.Tests.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.6.0-preview-20180109-01
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
[xUnit.net 00:00:00.4295013] Discovering: StreamstoneFs.Tests
[xUnit.net 00:00:00.4750480] Discovered: StreamstoneFs.Tests
[xUnit.net 00:00:00.4792986] Starting: StreamstoneFs.Tests
[xUnit.net 00:00:00.5964013] Finished: StreamstoneFs.Tests
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 1.1835 Seconds
> dotnet xunit
Detecting target frameworks in StreamstoneFs.Tests.fsproj...
Building for framework netcoreapp2.0...
StreamstoneFs -> C:\Work\OSS\Streamstone.fs\src\StreamstoneFs\bin\Debug\netstandard2.0\StreamstoneFs.dll
StreamstoneFs.Tests -> C:\Work\OSS\Streamstone.fs\tests\StreamstoneFs.Tests\bin\Debug\netcoreapp2.0\StreamstoneFs.Tests.dll
Running .NET Core 2.0.0 tests for framework netcoreapp2.0...
xUnit.net Console Runner (64-bit .NET Core 4.6.00001.0)
Discovering: StreamstoneFs.Tests
Discovered: StreamstoneFs.Tests
Starting: StreamstoneFs.Tests
Finished: StreamstoneFs.Tests
=== TEST EXECUTION SUMMARY ===
StreamstoneFs.Tests Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 0.109s
我在这里做错了什么?
答案 0 :(得分:2)
只有在单元测试失败时才能显示输出:
type StackOverflow(output : ITestOutputHelper) =
[<Fact>]
member __.``Output shows up`` () =
output.WriteLine("hello world, from output")
printfn "Hello world, from printfn!"
Assert.True(false)
这将给出输出:
Failed Tests+StackOverflow.Output shows up
Error Message:
Assert.True() Failure
Expected: True
Actual: False
Stack Trace:
at Tests.StackOverflow.Output shows up() in C:\git\dotnetTesting\Tests.fs:line 17
Standard Output Messages:
hello world, from output
无论测试结果如何,printfn
输出都不会显示。如果测试通过,则输出也不会显示。
从the docs开始,看起来输出是为调试而设计的,所以这是有道理的。在我看来,像dotnet命令行工具不能像Visual Studio在他们的例子中那样捕获输出。
答案 1 :(得分:1)
通过使用import numpy as np
import time
shape = ( 20, 30, 40 )
ndata = int( 1e6 )
data = np.random.normal( loc = 10, scale = 5, size = ndata )
coords = np.vstack( [ np.random.uniform( 0, shape[i], ndata )
for i in range( len( shape ) ) ] ).T
max_data = np.zeros( shape )
start = time.time()
for i in range( len( data ) ) :
# shortcut to find bin indices when the bins are
# [ range( shape[i] ) for i in range( len( shape ) ) ]
bin_indices = tuple( coords[i].astype( int ) )
max_data[ bin_indices ] = max( max_data[ bin_indices ], data[ i ] )
elapsed = time.time() - start
print( 'elapsed: %.3e' % elapsed ) # 2.98 seconds on my computer
Console.SetOut
这是在Rider中工作的地方;它也可以在Visual Studio 2019中使用。我尚未在VSCode / Ionide中对其进行过测试。
我的答案结合了this和this。我不必为module StackOverflowTests
open System.IO
open System
open Xunit
open Xunit.Abstractions
type Converter(output: ITestOutputHelper) =
inherit TextWriter()
override __.Encoding = stdout.Encoding
override __.WriteLine message =
output.WriteLine message
override __.Write message =
output.WriteLine message
type StackOverflow(output : ITestOutputHelper) =
do new Converter(output) |> Console.SetOut
[<Fact>]
member __.``Output shows up, for real`` () =
output.WriteLine "ITestOutputHelper"
printfn "printfn"
printf "printf"
Console.WriteLine("Console.WriteLine")
做任何事情。
答案 2 :(得分:0)
这在带有.NET Core项目的VS Code中有效,尽管有点吵:
dotnet test --logger:"console;verbosity=detailed"