【问题标题】:Run NUnit tests in Appcenter?在 Appcenter 中运行 NUnit 测试?
【发布时间】:2019-10-20 18:11:14
【问题描述】:

我有一个 Xamarin 项目,我会定期将其推送到 Appcenter,并在那里构建它。这很好用,但现在我在我的解决方案中添加了 NUnit 3 测试,虽然它们在本地执行得很好,但它们似乎在 Appcenter 构建期间没有被执行。

如何配置我的解决方案以便在 Appcenter 上执行我的测试项目?似乎必须combine it with Xamarin.UITest,但我真的不明白为此需要哪些步骤。请注意,我的 NUnit 测试不是 UI 测试,它们是普通的单元测试。

更新: 引用https://devblogs.microsoft.com/appcenter/faster-android-tests-and-nunit-3/

现在您可以将您的 NUnit 包与 UITest 一起更新到最新版本,并在本地和 App Center 中运行测试,

所以我希望在 App Center 构建期间运行 NUnit 测试而不需要额外的脚本。有人能对此有所了解吗?现在添加赏金。

【问题讨论】:

    标签: xamarin xamarin.android nunit nunit-3.0 visual-studio-app-center


    【解决方案1】:

    我发现让 AppCenter 运行项目的 NUnit 测试的最简单方法是为每个应用添加一个构建后脚本,并将 NunitXml.TestLogger Nuget 包安装到您的 NUnit 项目中,这将输出一个包含测试结果的 Xml 文件。

    要创建构建后脚本,它需要位于 Android/iOS .csproj 的根目录中并命名为 appcenter-post-build.sh。那么你的脚本应该是这样的:

    #Android post build script
    #Make sure the directly to the NUnit csproj is correct
    ProjectPath="$APPCENTER_SOURCE_DIRECTORY\YourProject.NUnit\YourProject.NUnit.csproj"
    echo "$ProjectPath"
    #To generate the xml file it requires nuget NunitXml.TestLogger
    dotnet test "$APPCENTER_SOURCE_DIRECTORY" --logger:"nunit;LogFilePath=TestResults.xml"
    echo "NUnit tests result:"
    pathOfTestResults=$(find $APPCENTER_SOURCE_DIRECTORY -name 'TestResults.xml')
    cat $pathOfTestResults
    echo
    
    #Looks for a failing test and causes the build to fail if found
    grep -q 'result="Failed"' $pathOfTestResults
    
    if [[ $? -eq 0 ]]
    then 
    echo "A test Failed" 
    exit 1
    else 
    echo "All tests passed" 
    fi
    

    如果测试失败,最后一部分应该会导致您的 AppCenter 构建失败。此外,您可能需要在 AppCenter 中尝试构建它两次,然后它才会发现已将构建后脚本添加到您的存储库中。

    【讨论】:

    • 谢谢,这看起来是个好方法,但我解释了这篇博文devblogs.microsoft.com/appcenter/…“现在您可以将您的 NUnit 包与 UITest 一起更新到最新版本,并在本地和应用中心运行测试”,因为有一种更正式的方式,可能没有脚本。这就是为什么我还没有选择这个作为答案。
    • 如果我弄错了,请有人纠正我,但我认为要使用正式版本,您必须为 AppCenter 中的测试层付费。否则,您必须设置一个构建脚本来运行您的单元测试。
    【解决方案2】:

    我对博客的理解是 UITest 曾经是基于 nunit 2.x 的。因此,以前如果您不小心更新到 nunit3.x,您的 UITests 将无法工作。

    他们现在对其进行了更新,以便您可以使用 nunit 3.x 来运行您的 UI 测试。

    我相信@Nick Peppers 给出了正确的方法。此处的示例后期构建脚本: https://github.com/microsoft/appcenter/blob/master/sample-build-scripts/xamarin/nunit-test/appcenter-post-build.sh

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2012-03-07
      • 2019-03-04
      相关资源
      最近更新 更多