question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotnet test/run have inconsistent CWD

See original GitHub issue

From @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}]");
    }
}
  1. Run the app: dotnet run
  2. Note the output of the app.
  3. Run the unit test with logging to capture the output: dotnet test --logger "trx;LogFileName=C:\some\path\Results.trx"
  4. 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:open
  • Created 4 years ago
  • Reactions:5
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
PhilTcommented, May 1, 2020

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 with run and inconsistent with expected behaviour of GetCurrentDirectory().

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 and dotnet run tests which gives prettier output for local dev work. The tests rely on some data in the project but I can’t rely on GetCurrentDirectory() if I want the CI dotnet test to work.

Please consider reopening this. Thanks.

0reactions
andradfcommented, Jan 19, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found