MSBUILD 针对匹配项目运行 NCover

发布于 2024-10-16 04:32:12 字数 894 浏览 5 评论 0原文

我正在使用 MSBuild,并在项目名称上使用正则表达式获取所有测试项目,如下所示。

<RegexMatch Input="@(AllProjects)" Expression="(.)*Test(.)*">
    <Output  TaskParameter="Output" ItemName="UnitTestProjects"/>
</RegexMatch>

我现在想使用 @(UnitTestProjects) 并将它们全部传递给 NCover 以检查测试是否都提供 100% 的覆盖率。

要在单个项目上执行此操作,我会执行以下操作:

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
        TestRunnerArgs="&quot;C:\SomeProject\bin\SomeProject.dll&quot; &quot;C:\SomeProject\bin\SomeProjectTest.dll&quot;"
        WorkingDirectory="C:\SomeProject\bin\"
        AppendTrendTo="coverage.trend"
        OnlyAssembliesWithSource="True"
        ProjectName="SomeProjectCoverage"    />
</Target>

How do I effective use @(UnitTestProjects) in this NCover target?

I am using MSBuild and am getting all test projects using a regex on the project name, like this.

<RegexMatch Input="@(AllProjects)" Expression="(.)*Test(.)*">
    <Output  TaskParameter="Output" ItemName="UnitTestProjects"/>
</RegexMatch>

I now want to use @(UnitTestProjects) and pass them all to NCover to check that the tests are all giving 100% coverage.

To do this on a single project, I do something like this:

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
        TestRunnerArgs=""C:\SomeProject\bin\SomeProject.dll" "C:\SomeProject\bin\SomeProjectTest.dll""
        WorkingDirectory="C:\SomeProject\bin\"
        AppendTrendTo="coverage.trend"
        OnlyAssembliesWithSource="True"
        ProjectName="SomeProjectCoverage"    />
</Target>

How do I effectively use @(UnitTestProjects) in the context of this NCover target?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2024-10-23 04:32:12

为了批处理您的 @(UnitTestProjects) ,您的 Target 必须像这样使用它:

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
    TestRunnerArgs=""%(UnitTestProjects.Identity)" "%(UnitTestProjects.Identity)""

<!--
If you receive the Task Output (*Test.dll) you will have to extract
the working dir path
-->
    WorkingDirectory="C:\SomeProject\bin\"
    AppendTrendTo="coverage.trend"
    OnlyAssembliesWithSource="True"
<!--
... the same thing applies to finding out the current project name
from your Task Ouput.
-->
    ProjectName="SomeProjectCoverage"    />
</Target>

更可靠的解决方案是提供 TaskItems 列表,保存有关您的(测试)项目的元数据。

<ItemGroup>
    <TestProject Include="MyProject1.Test.dll">
        <TestProjectName>MyProject1</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject1\bin</MyTestProjectWorkingDir>
    </TestProject>
    <TestProject Include="MyProject2.Test.dll">
        <TestProjectName>MyProject2</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject2\bin</MyTestProjectWorkingDir>
    </TestProject>
</ItemGroup>

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
    TestRunnerArgs=""%(TestProject.Identity)" "%(TestProject.Identity)""
    WorkingDirectory="%(TestProject.MyTestProjectWorkingDir)"
    AppendTrendTo="coverage.trend"
    OnlyAssembliesWithSource="True"
    ProjectName="%(TestProject.MyTestProjectName)"    />
</Target>

您的问题表明您正在寻找一种自动化方法,该方法允许您添加新的测试项目,而无需维护任务项的配置列表。

由于提取 NCover 任务所需的所有信息非常困难,因此半自动化方法可能适合您。

您可以向您的测试项目添加一个导入,该导入将提供给您的全局测试项目 ItemGroup:

<ItemGroup>
    <!-- add existing ItemGroup -->
    <TestProject Include="@(TestProject)" />

    <!-- add project itself -->
    <TestProject Include="MyProject1.Test.dll">
        <TestProjectName>MyProject1</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject1\bin</MyTestProjectWorkingDir>
    </TestProject>
</ItemGroup>

NCover 任务中的用法将与上面相同。

这样您的主构建脚本不需要了解任何特定的测试项目;它只是处理您的 ItemGroup“TestProject”。

For batching your @(UnitTestProjects) your Target will have to use it like this:

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
    TestRunnerArgs=""%(UnitTestProjects.Identity)" "%(UnitTestProjects.Identity)""

<!--
If you receive the Task Output (*Test.dll) you will have to extract
the working dir path
-->
    WorkingDirectory="C:\SomeProject\bin\"
    AppendTrendTo="coverage.trend"
    OnlyAssembliesWithSource="True"
<!--
... the same thing applies to finding out the current project name
from your Task Ouput.
-->
    ProjectName="SomeProjectCoverage"    />
</Target>

A more reliable solution would be, to provide a list of TaskItems, holding metadata about your (Test)project.

<ItemGroup>
    <TestProject Include="MyProject1.Test.dll">
        <TestProjectName>MyProject1</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject1\bin</MyTestProjectWorkingDir>
    </TestProject>
    <TestProject Include="MyProject2.Test.dll">
        <TestProjectName>MyProject2</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject2\bin</MyTestProjectWorkingDir>
    </TestProject>
</ItemGroup>

<Target Name="Coverage">
    <NCover TestRunnerExe="C:\Program Files\NUnit 2.5.8\bin\net-2.0\nunit-console.exe"
    TestRunnerArgs=""%(TestProject.Identity)" "%(TestProject.Identity)""
    WorkingDirectory="%(TestProject.MyTestProjectWorkingDir)"
    AppendTrendTo="coverage.trend"
    OnlyAssembliesWithSource="True"
    ProjectName="%(TestProject.MyTestProjectName)"    />
</Target>

Your question suggests that you are looking for an automated approach which will allow you to add new test projects without having to maintain a configuration list of TaskItems.

Since it would be quite difficult, to extract all the information needed to feed your NCover Task maybe a semi-automated approach might work for you.

You could add an import to your test project which will feed your global test project ItemGroup:

<ItemGroup>
    <!-- add existing ItemGroup -->
    <TestProject Include="@(TestProject)" />

    <!-- add project itself -->
    <TestProject Include="MyProject1.Test.dll">
        <TestProjectName>MyProject1</TestProjectName>
        <MyTestProjectWorkingDir>C:\MyProject1\bin</MyTestProjectWorkingDir>
    </TestProject>
</ItemGroup>

The usage in your NCover Task will be the same as above.

This way your master build script doesn't need to know about any specific test project; it just processes your ItemGroup "TestProject".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文