-
Notifications
You must be signed in to change notification settings - Fork 388
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
coverlet.collect covering NUnit tests and resulting with 0% line coverage #1503
Comments
Hi, I use reportgenerator to filter source files as well because some generated files like |
Thanks for the follow up! I'm not 100% sure how to know which package I use, so tell me if my logic is wrong. My guess is that I use |
Yes, you are using
|
It's a bit hard to say why in your case the test classes are part of the coverage result. By default the coverage of the test assembly shouldn't be part of the result. If this still happens like in your case, you can try to use filters to reduce the noise in the overall coverage e.g. When I look into your snipped I wonder a bit where your productive code is. I don't see any other reference than
So does the type |
Yeah, I guess my questions would be:
As a workaround, I think I can simply put a
Not the same assembly (tests are in their own project), but the same namespace, this is why I don't need a specific |
@daveMueller I made a synthetic test case: https://github.com/romainf-ubi/bug-tests-in-coverage The steps to reproduce are explained the README.md Here is the result: As you can see, the tests class |
@daveMueller @Bertk did you have time to look at the repo I made to reproduce the bug? It should be enough to remove the "question" and "waiting for customer" tags |
This is actually the next issue I wanted to look into but was occupied with other things. 🙏 I don't have in mind how we distinguish test projects from productive projects right now but I will look into it. Could be that there are problems because the projects share the same namespace. But I will look into it and give you some feedback once I figured it out. The repro will definitely help. thanks a lot. 👍 |
Completely understand that you have other priorities right now, no worries 😉 Thanks for the heads up! |
I am not sure whether this is acceptable for your scenario but you can use assembly filters. A) Using coverlet.collector Exclude option (special CLI syntax " -- " separator for command and settings)
B) Using reportgenerator assemblyfilters option
|
It could be a workaround, but I think this issue should still be considered as a bug since test methods should not be included in the test coverage. Also imagine that our projects are a bit more complex than the repro given above 😅 We often have more than 5 test projects per solution, and we have around a dozen of solutions in our ecosystem, and we are sharing CI/CD scripts between repositories. So the workaround would quickly hinder the readability and maintainability of our projects. Right now, we do with the "false" coverage score; we know the real score is a bit better than that. But I think that fixing this issue would allow coverlet users to have more deterministic results in the end. |
I am not sure. One of the test projects does not have any implementation (*.cs) and the SDK is generating one main method. |
Duplicate of #604 Microsoft.NET.Test.Sdk has
You can exclude this with
|
I try to explain what I could observe at least in your repro. In general coverlet runs per project and calculates a coverage report for this project. This is also how the dotnet test runners work. dotnet test ".\tests\MyApp.Infrastructure.Tests\MyApp.Infrastructure.Tests.csproj" -c Debug /p:CollectCoverage=true
Now to answere your question, you can see the test There maybe is the misconception that coverlet can detect unit test methods and exclude them from the coverage calculation. This is not the case. We can't check the IL for attributes like To your initial question. For me it seems that this report wasn't the result of
|
As @Bertk already mentioned #1503 (comment). The class |
@Bertk and @daveMueller:
The test case is simple because I tried to make the simplest, smallest project to reproduce the issue. So it's not really an edge case as I can ensure you that it happens to me on a much more complex project with several test projects and hundreds of tests in them. Please understand that the test case I made here is but a mere test case and that, by definition, it will look like an edge case as it includes the strict minimum to trigger the issue. The goal of this test case is to make it easier for you to debug and find the issue, by removing all possible noise around it. Just to be sure, I've added a test in @daveMueller: Thanks for the explanations. If I understand well, the root of the issue is that one test project is referencing another test project. If I create a third project which would contain the common code of the other two test project, it should fix the issue. Is that it? For the rest of the explanations, I have a few questions:
Wouldn't it be possible to exclude the full name? (i.e. namespace NUnit.Framework
{
[ExcludeFromCodeCoverage] // <-- they'll need to add an attribute (this one or another one)
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class TestAttribute : NUnitAttribute, ISimpleTestBuilder, IApplyToTest, IImplyFixture
{
/* ... */
}
}
You've kinda lost me here... But from your explanations, I can tell you that, in my private project, |
About the |
Yes exactly.
Yes this would work but we would only benefit from it if every test library would use the same attribute. Also there is the problem that some people want to collect coverage for their test classes. dotnet test ".\MyApp.Infrastructure.Tests.csproj" -c Debug --collect:"XPlat Code Coverage;Format=cobertura" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.ExcludeByAttribute=TestAttribute,SetUpAttribute or use the runsetting file.
Yes exactly. |
Ok thanks! I think this issue can be closed as it answered my questions and I don't think there is anything to implement as it is the intended behavior (except maybe a note in the documentation about what is automatically excluded and what's not?) |
Hi!
TL;DR: some tests are part of the code coverage and some aren't, I don't understand why (tests should not be part of the code coverage IMHO).
I can't share my code as it is proprietary, but I can explain my issue here.
I have several projects in the
src/
directory, and each project has it's test counterpart in thetests/
directory. So, for instance, if I havesrc/MyProject/MyProject.csproj
then I'll havetests/MyProject.Tests/MyProject.Tests.csproj
.Here are the packages I use for my test project:
In my solution, I have 3 projects in the
src/
directory, and 4 projects in mytests/
directory (the extra project is a shared one for common tests utilities).I use reportgenerator to get a nice Cobertura file that I can integrate with GitLab later.
My issue is this: one test project reports that the tests themselves (i.e. with the
[Test]
attribute) is not covered and I don't understand why.So I get this report:
I do understand that the
Randomizer
classes are covered as it is just utility methods without any[Test]
attribute, but I don't understand whySubmitJobCommandTests
,GetAllJobsQueryTests
andGetJobByIdQueryTests
have a 0% score for the line coverage.Here's how
SubmitJobCommandTests.cs
looks like:As you can see, it's fairly simple and there are no other methods that could explain why this file would be part of the code coverage. Also, the other test projects don't appear in the code coverage (and I didn't add any
[ExcludeFromCodeCoverage]
attributes).The text was updated successfully, but these errors were encountered: