FortuneN / FineCodeCoverage

Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage
Other
522 stars 40 forks source link

Coverage fails for file paths relative to the Nunit test directory property #324

Closed prw56 closed 1 year ago

prw56 commented 1 year ago

Installed product versions

Description

When writing tests with paths that depend on the Nunit TestContext.CurrentContext.TestDirectory property, coverage will fail b/c the value of the property at coverage time is different from test execution time.

Steps to recreate

  1. create a Test
  2. In the test, store the value of TestContext.CurrentContext.TestDirectory (something like this, sorry I can't get the formatting to behave) var curDir = TestContext.CurrentContext.TestDirectory; File.AppendAllText(@"C:\Users\Skull\Desktop\output.txt", $"\n{curDir}");
  3. Run the test, then wait a bit for coverage to run, then look at the output, you'll see:

normalTestPath normalTestPath\fine-code-coverage\build-output

Current behavior

When coverage runs the tests "\fine-code-coverage\build-output" is appended to the end of TestContext.CurrentContext.TestDirectory

Expected behavior

Value of TestContext.CurrentContext.TestDirectory should be the same at coverage time as run time.

tonyhallett commented 1 year ago

Use ms code coverage or look at AdjacentBuildOutput

prw56 commented 1 year ago

@tonyhallett Was the issue fixed? I assumed your answer was a workaround

tonyhallett commented 1 year ago

The behaviour you see is documented and cannot be changed. Ms code coverage does not require dll copying, runs once and runs only the tests that you select.

tonyhallett commented 1 year ago

If you really need the old style then AdjacentBuildOutput for a Net 6 project:

File.AppendAllText(outputPath, TestContext.CurrentContext.TestDirectory);

\DemoFCC\TestProject1\bin\Debug\net6.0 \DemoFCC\TestProject1\bin\Debug\fine-code-coverage-net6.0

Perhaps this will suffice.

Or if you are accessing resources then embed them in the test assembly https://korzh.com/blog/embedded-resources-testing-projects