dotnet test/run have inconsistent CWD
See original GitHub issueFrom @yoshiya8 on Monday, 27 August 2018 15:10:42
Steps to reproduce
Create a .NET core app that simply prints out the current directory from the environment and a .NET core XUNIT test case that does the same:
using System;
namespace DotnetRun
{
class Program
{
static void Main(string[] args) => Console.WriteLine($"CWD: [{Environment.CurrentDirectory}]");
}
}
and
using System;
using Xunit;
using Xunit.Abstractions;
namespace DotnetTest
{
public class UnitTest1
{
private readonly ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output) => this.output = output;
[Fact]
public void Test1() => output.WriteLine($"CWD: [{Environment.CurrentDirectory}]");
}
}
- Run the app:
dotnet run
- Note the output of the app.
- Run the unit test with logging to capture the output:
dotnet test --logger "trx;LogFileName=C:\some\path\Results.trx"
- Get the output from the unit test:
Select-String -Path C:\some\path\Results.trx -Pattern "<StdOut>"
Expected behavior
The current directory for each run would be the current directory of the shell that launched the dotnet
command.
Actual behavior
The current directory of the dotnet run
is as expected. However, the current directory of the dotnet test
is the directory of the .dll
file (“$pwd\bin\Debug\netcorapp2.0
”)
Copied from original issue: dotnet/cli#9896
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:11 (3 by maintainers)
Top Results From Across the Web
dotnet test/run have inconsistent CWD · Issue #9695
NET core app that simply prints out the current directory from the environment and a .NET core XUNIT test case that does the...
Read more >cargo test - The Cargo Book
When no target selection options are given, cargo test will build the following targets of the selected packages: lib — used to link...
Read more >Run selected unit tests - .NET
How to use a filter expression to run selected unit tests with the dotnet test command in .NET Core.
Read more >CWD test results: July 1, 2023 to present | Minnesota DNR
Current season sampling results; View interactive map of CWD-positive wild deer; Tribal sampling results; Collect your own sample for testing.
Read more >Specflow table multiple rows. I have a scenario outline with
I have a scenario outline with an example table with multiple rows having unique test parameters that are being used for … It...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s a shame this has been closed. I still think this issue is valid.
When users call
System.IO.Directory.GetCurrentDirectory()
they expect to get the current directory. In other words the one that was current when executing the command,dotnet test
. This is neither the output directory nor the source directory. It tends to be the solution directory. It’s inconsistent withrun
and inconsistent with expected behaviour ofGetCurrentDirectory()
.Also, regarding the code,
sourceDirectory
is not correct either as I believe it’s actually the proj directory which, by default is copied to the output dir anyway which is why CWD ends up being the output dir.If you want a specific scenario, mine is that I have 2 ways to run my tests.
dotnet test
for CI anddotnet run tests
which gives prettier output for local dev work. The tests rely on some data in the project but I can’t rely onGetCurrentDirectory()
if I want the CIdotnet test
to work.Please consider reopening this. Thanks.
I just came across this too. While the argument that the test environment shouldn’t impact the tests is completely valid for unit tests, for integration tests and bechmarking it is just the opposite. We often want to run the same set of tests with different environment configurations.