F#:如何在 mstest 单元测试中包含外部测试数据
F#: How to include external test data in mstest unit tests
我想用外部文件中定义的一些数据测试我的代码。
我尝试了以下方法:
namespace blub
open System
open Microsoft.VisualStudio.TestTools.UnitTesting
[<TestClass>]
type TestClass () =
[<TestMethod>]
member this.TestMethodPassing () =
let txt = System.IO.File.ReadAllText "data.txt"
Assert.IsTrue(txt.Contains "Hello");
我刚刚使用 dotnet new mstest -lang F#
创建了项目,并将 data.txt
文件放在 Test.fs
文件旁边。
但是,当我 运行 使用 dotnet test
进行测试时,出现以下错误:
Failed TestMethodPassing
Error Message:
Test method blub.TestClass.TestMethodPassing threw exception:
System.IO.FileNotFoundException: Could not find file '/home/peter/Desktop/blub/bin/Debug/netcoreapp2.1/data.txt'.
Stack Trace:
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.File.InternalReadAllText(String path, Encoding encoding)
at System.IO.File.ReadAllText(String path)
at blub.TestClass.TestMethodPassing() in /home/peter/Desktop/blub/Tests.fs:line 11
我当然可以通过将路径更改为 "../../../data.txt"
来解决此问题,但这似乎不是一个稳定的解决方案——我没有找到任何说明测试执行如何影响当前目录的文档。
我能否以某种方式将我的测试文件声明为要复制到正确文件夹的资源?
您需要将 data.txt
文件添加到 fsproj 并将其设置为 :
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
如果仍然找不到,您可能需要使用 [<DeploymentItem("data.txt")>]
对 TestClass
。
这会将文件从输出文件夹复制到执行测试的文件夹。
我想用外部文件中定义的一些数据测试我的代码。 我尝试了以下方法:
namespace blub
open System
open Microsoft.VisualStudio.TestTools.UnitTesting
[<TestClass>]
type TestClass () =
[<TestMethod>]
member this.TestMethodPassing () =
let txt = System.IO.File.ReadAllText "data.txt"
Assert.IsTrue(txt.Contains "Hello");
我刚刚使用 dotnet new mstest -lang F#
创建了项目,并将 data.txt
文件放在 Test.fs
文件旁边。
但是,当我 运行 使用 dotnet test
进行测试时,出现以下错误:
Failed TestMethodPassing
Error Message:
Test method blub.TestClass.TestMethodPassing threw exception:
System.IO.FileNotFoundException: Could not find file '/home/peter/Desktop/blub/bin/Debug/netcoreapp2.1/data.txt'.
Stack Trace:
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.File.InternalReadAllText(String path, Encoding encoding)
at System.IO.File.ReadAllText(String path)
at blub.TestClass.TestMethodPassing() in /home/peter/Desktop/blub/Tests.fs:line 11
我当然可以通过将路径更改为 "../../../data.txt"
来解决此问题,但这似乎不是一个稳定的解决方案——我没有找到任何说明测试执行如何影响当前目录的文档。
我能否以某种方式将我的测试文件声明为要复制到正确文件夹的资源?
您需要将 data.txt
文件添加到 fsproj 并将其设置为
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
如果仍然找不到,您可能需要使用 [<DeploymentItem("data.txt")>]
对 TestClass
。
这会将文件从输出文件夹复制到执行测试的文件夹。