F#: How to include external test data in mstest unit tests
I want to test my code with some data defined in an external file.
I tried the following:
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");
I just created the project with dotnet new mstest -lang F#
and put the data.txt
file next to the Test.fs
file.
However, when I run the tests with dotnet test
I get the following error:
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
I can of course fix this by changing the path to "../../../data.txt"
, but this does not seem like a stable solution -- I did not find any documentation that states how test execution affects the current directory.
Can I somehow declare my test file as a resource to be copied to the correct folder?
f# .net-core mstest
add a comment |
I want to test my code with some data defined in an external file.
I tried the following:
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");
I just created the project with dotnet new mstest -lang F#
and put the data.txt
file next to the Test.fs
file.
However, when I run the tests with dotnet test
I get the following error:
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
I can of course fix this by changing the path to "../../../data.txt"
, but this does not seem like a stable solution -- I did not find any documentation that states how test execution affects the current directory.
Can I somehow declare my test file as a resource to be copied to the correct folder?
f# .net-core mstest
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11
add a comment |
I want to test my code with some data defined in an external file.
I tried the following:
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");
I just created the project with dotnet new mstest -lang F#
and put the data.txt
file next to the Test.fs
file.
However, when I run the tests with dotnet test
I get the following error:
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
I can of course fix this by changing the path to "../../../data.txt"
, but this does not seem like a stable solution -- I did not find any documentation that states how test execution affects the current directory.
Can I somehow declare my test file as a resource to be copied to the correct folder?
f# .net-core mstest
I want to test my code with some data defined in an external file.
I tried the following:
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");
I just created the project with dotnet new mstest -lang F#
and put the data.txt
file next to the Test.fs
file.
However, when I run the tests with dotnet test
I get the following error:
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
I can of course fix this by changing the path to "../../../data.txt"
, but this does not seem like a stable solution -- I did not find any documentation that states how test execution affects the current directory.
Can I somehow declare my test file as a resource to be copied to the correct folder?
f# .net-core mstest
f# .net-core mstest
asked Jan 3 at 18:33
peqpeq
739616
739616
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11
add a comment |
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
You will need to add the data.txt
file to the fsproj and set it to copy to the output folder:
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If it still isn't found, you may you need to use the [<DeploymentItem("data.txt")>]
against the TestClass
.
This will copy the files from the output folder to the folder where the tests are executed.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027906%2ff-how-to-include-external-test-data-in-mstest-unit-tests%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You will need to add the data.txt
file to the fsproj and set it to copy to the output folder:
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If it still isn't found, you may you need to use the [<DeploymentItem("data.txt")>]
against the TestClass
.
This will copy the files from the output folder to the folder where the tests are executed.
add a comment |
You will need to add the data.txt
file to the fsproj and set it to copy to the output folder:
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If it still isn't found, you may you need to use the [<DeploymentItem("data.txt")>]
against the TestClass
.
This will copy the files from the output folder to the folder where the tests are executed.
add a comment |
You will need to add the data.txt
file to the fsproj and set it to copy to the output folder:
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If it still isn't found, you may you need to use the [<DeploymentItem("data.txt")>]
against the TestClass
.
This will copy the files from the output folder to the folder where the tests are executed.
You will need to add the data.txt
file to the fsproj and set it to copy to the output folder:
<ItemGroup>
<Content Include="data.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If it still isn't found, you may you need to use the [<DeploymentItem("data.txt")>]
against the TestClass
.
This will copy the files from the output folder to the folder where the tests are executed.
answered Jan 3 at 21:29
DaveShawDaveShaw
41k1191126
41k1191126
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027906%2ff-how-to-include-external-test-data-in-mstest-unit-tests%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the binary is executed from the bin. Update project to copy data file to output so it is in the correct location when the code executes
– Nkosi
Jan 3 at 19:11