Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems using NUnit3 TestAdapter. #101

Closed
basilfx opened this issue May 18, 2018 · 4 comments · Fixed by #102
Closed

Problems using NUnit3 TestAdapter. #101

basilfx opened this issue May 18, 2018 · 4 comments · Fixed by #102

Comments

@basilfx
Copy link

basilfx commented May 18, 2018

Using the workaround mentioned in #40, I was able to at least run using the NUnit3 test adapter. However, I still have to blacklist/exclude inserting instrumentation code into the test adapter code, otherwise it cannot find the tests.

Now that 2.0 is available, I can call it like this: dotnet test /p:CollectCoverage=true /p:Exclude=[NUnit3.TestAdapter]*, which lets me run the tests (and after disassembling the DLLs, I see that they have been modified). However, the coverage remains 0%.

I can imagine that Coverlet wants to be test adapter agnostic, but any help/tips/idea's to get it up and running?

@MaartenX
Copy link
Contributor

The reason the results are not found is that the CoverageTracker.CurrentDomain_ProcessExit is never called when using the test adapter. I'm looking into other ways to detect the tests have finished, currently I have a workaround, I just call the method after my tests are finish using reflection.

Add the following class to your test project:

using System;
using System.Linq;
using NUnit.Framework;

namespace NUnitCoverletFix
{
    /// <summary>
    /// Fixes coverage for the NUnit3 TestAdapter by reporting the 'end of process' to Coverlet.
    /// </summary>
    [SetUpFixture]
    public class NUnitCoverageFix
    {
        /// <summary>
        /// Called when the NUnit tests are done. Tries to call Coverlet.Core.CoverageTracker.CurrentDomain_ProcessExit
        /// by using reflection. This DLL is only loaded when we're using coverlet, and this method is
        /// only called when using NUnit, so this is for a very specific case.
        /// </summary>
        [OneTimeTearDown]
        public void GlobalTeardown()
        {
            var coverageTracker = AppDomain.CurrentDomain.GetAssemblies()
                .FirstOrDefault(
                    ga => ga.GetName().Name.Equals("Coverlet.Core", StringComparison.InvariantCultureIgnoreCase))
                ?.ExportedTypes
                .FirstOrDefault(
                    t => t.FullName == "Coverlet.Core.CoverageTracker");

            coverageTracker
                ?.GetMethod("CurrentDomain_ProcessExit")
                ?.Invoke(null, new object[] { AppDomain.CurrentDomain, EventArgs.Empty });
        }
    }
}

@basilfx
Copy link
Author

basilfx commented May 18, 2018

Thanks for the fast reply @MaartenX, I’ll check of this works for me as well.

@basilfx
Copy link
Author

basilfx commented May 18, 2018

I also think #24 is related.

@basilfx
Copy link
Author

basilfx commented May 18, 2018

This works for me, but I have to add it to every test project (teardown doesn’t seem to be called when it’s in a sub project).

mburumaxwell pushed a commit to faluapp/falu-dotnet that referenced this issue Jun 12, 2021
Bumps [coverlet.collector](https://github.com/coverlet-coverage/coverlet) from 1.3.0 to 3.0.1.

#Release notes

*Sourced from [coverlet.collector's releases](https://github.com/coverlet-coverage/coverlet/releases).*

> ## v3.0.0
> * [#131](coverlet-coverage/coverlet#131) makes a slight change to the Coverlet JSON format
> * 807f7b1bd5bea8158ffff343d5511cd16e0da9a0 uses a separate `coverlet.tracker` assembly to hold tracking code
> * [#128](coverlet-coverage/coverlet#128) adds support for assemblies with `.exe` extension
> * a1f18b4156374f3398d704e898ec58c7c6c64bf8 improves identifying compiler generated types
> * [#134](coverlet-coverage/coverlet#134) adds considerable coverage tracking performance improvements
>
> ## v2.0.1
> * [#102](coverlet-coverage/coverlet#102) fixes issues with NUNIT3 Test adapter ([#101](coverlet-coverage/coverlet#101))
> * [#104](coverlet-coverage/coverlet#104) shows overall averages as part of final console output
> * [#112](coverlet-coverage/coverlet#112) adds support for standard `ExcludeFromCodeCoverage` attribute to specify types and methods to exclude from code coverage. Deprecates `ExcludeFromCoverage` attribute
> * coverlet-coverage/coverlet@7f190e4 prevents Opencover and Cobertura output generated at the same time from overwriting each other ([#111](coverlet-coverage/coverlet#111))
> * [#116](coverlet-coverage/coverlet#116) strongly signs the Coverlet assembly and aims to fix [#40](coverlet-coverage/coverlet#40)
>
> ## v2.0.0
> * [#78](coverlet-coverage/coverlet#78) adds support for generating multiple report formats in a single run
> * [#73](coverlet-coverage/coverlet#73) improves branch coverage support and output formats*
> * coverlet-coverage/coverlet@d2effb3 shows method coverage in summary output
> * [#88](coverlet-coverage/coverlet#88) improves disk usage by using gzip compression
> * [#93](coverlet-coverage/coverlet#93) adds `ThresholdType` property that allows you to specify the coverage type to apply the `Threshold` property to
> * coverlet-coverage/coverlet@ebedd70 renames `Exclude` property to `ExcludeByFile`*
> * coverlet-coverage/coverlet@9ed0864 supports using filter expressions to exclude assemblies, namespaces or types. Uses the `Exclude` property*
> * [#99](coverlet-coverage/coverlet#99) adds improvements to evaluation of filter expressions
>
> `*` - Backwards incompatible change

#Commits

- See f...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants