SteveGilham / altcover

Cross-platform coverage gathering and processing tool set for dotnet/.Net Framework and Mono
MIT License
501 stars 18 forks source link

Adding AltCover to projects results in dotnet test running against all projects in solution #209

Closed nmoinvaz closed 6 months ago

nmoinvaz commented 8 months ago

When I add the AltCover package to all the projects in my solution, the dotnet test command will run all projects in the solution as if they were tests. Without the AltCover package, dotnet test only runs against projects that are test projects with names ending in *.Test. Hopefully it is easy to spot what I am doing wrong.

I end up having to resort to implementing something like this to bypass it. https://dasmulli.blog/2018/01/20/make-dotnet-test-work-on-solution-files/

SteveGilham commented 7 months ago

Apologies for tardy response.

It's been some while since I last referred back to the underlying SDK VSTest target, and having just done so, I see some of the logic around the IsTestProject property has changed

You write

I add the AltCover package to all the projects in my solution

It's only necessary to add the package to the test projects as those are the ones that make use of the modified VSTest; for the moment that should, hopefully, be a suitable work-round until I update the targets file..

SteveGilham commented 7 months ago

This should be resolved in the next release.

nmoinvaz commented 7 months ago

It's only necessary to add the package to the test projects as those are the ones that make use of the modified VSTest;

In #208 I had mentioned that I need to run both the VSTest project for coverage but also console projects that don't use VSTest. Should altcover be added to those console projects also? Dependent libraries of either VSTest or console projects wouldn't need altcover added correct?

This should be resolved in the next release.

Thank you!

SteveGilham commented 7 months ago

The altcover nuget package is primarily a command-line tool. Unless you are doing your own MSBuild scripting invoking the altcover MSBuild tasks, the only reason to add it to an individual project is to include the .targets file overriding the VSTest build target within the dotnet test operation. So for a project that looks like this

Screenshot 2024-04-09 083222

where the unit test project runs tests on the console project, adding altcover to just the test project will allow dotnet test to cover both the test project and all the assemblies it links locally against except as explicitly excluded, or if lacking debug symbols. In this case, that includes the console application.

If what you are doing with the console projects is to do MSBuild scripting using the tasks, do let me know, as there is a better approach that I can implement,

SteveGilham commented 7 months ago

Release 8.8.21 now requires both $(AltCover) and $(IsTestproject) to be "true" before making the VSTest override.

nmoinvaz commented 6 months ago

Thanks for the clarification and for taking a look at it on your end. Instead of adding altcover package to every test project manually, I am decided to do the following in my GitHub workflow because it requires less maintenance:

- name: Add AltCover package for all test projects
  shell: bash
  run: |
    for dir in *.Test; do \
      if [ -d "$dir" ]; then
        dotnet add "$dir" package altcover --version 8.8.21; \
      fi
    done

- name: Build project
  shell: bash
  run: dotnet build <solution> --configuration Release

- name: Run unit tests
  shell: bash
  run: dotnet test <solution> --property:AltCover=true --property:AltCoverReportFormat=opencover