如何使用cakebuild.net?

时间:2018-04-30 15:41:06

标签: visual-studio xamarin.forms xunit cakebuild

我目前有一个Cake构建脚本,用于在我的解决方案中构建我的各个项目,并针对这些项目运行一些xUnit测试。我没有看到任何文档或示例采用Xamarin单元测试应用程序或UI测试构建DLL并使用Cake运行它们。我想知道这样的事情是否可能,如果是这样的话怎么样? 我的蛋糕脚本:

#addin "Cake.Xamarin"
#tool "nuget:?package=xunit.runner.console"
#addin "nuget:?package=HyperLinq"
#addin "nuget:?package=XunitXmlResultParser"

//using XunitXmlResultParser;
// Arguments.
var target = Argument ("target", "Default");
var configuration = Argument ("configuration", "release");

// Define directories.
var solutionFile = GetFiles ("./OSP_POC.sln").First ();

// Android and iOS.
var androidProject = GetFiles ("./OSP_POC/OSP_POC.Android/*.csproj").First ();
var iOSProject = GetFiles ("./OSP_POC/OSP_POC.iOS/*.csproj").First ();

// stylecop settings file
var settingsFile = File ("Settings.stylecop");

// xml results file
var resultFile = File ("StylecopResults.xml");
// html report file
var htmlFile = File ("StylecopResults.html");
// Tests.
var testsProject = GetFiles ("./OSP_UnitTests/OSP_UnitTests.csproj").First ();
// NOTE: (assumes Tests projects end with .Tests).
var testsDllPath = string.Format ("./OSP_UnitTests/bin/{0}/netcoreapp2.0/OSP_UnitTests.dll", configuration);

// Output folders.
var artifactsDirectory = Directory ("./artifacts");

Task ("Clean")
    .Does (() => {
        CleanDirectories (string.Format ("./**/obj/{0}",
            configuration));
        CleanDirectories (string.Format ("./**/bin/{0}",
            configuration));

        Information ("Finished cleaning");
    });

Task ("Restore-Packages")
    .Does (() => {
        NuGetRestore (solutionFile);
    });

/*Task("StyleCop")
  .Does(() =>
{
    StyleCopAnalyse(settings => settings
        .WithSolution(solutionFile)
        .WithSettings(settingsFile)
        .ToResultFile(resultFile)
        .ToHtmlReport(htmlFile)
    );
});*/

Task ("Run-Tests")
    // Allows the build process to continue even if there Tests aren't passing.
    .ContinueOnError ()
    .IsDependentOn ("Prepare-Build")
    .Does (() => {
        Information ("running tests");
        Information ($"{testsProject.FullPath}");
        MSBuild (testsProject.FullPath, settings => settings
            .SetConfiguration (configuration)
            .WithTarget ("Build")
            .SetVerbosity (Verbosity.Minimal));
        DotNetCoreTool (testsProject,
            "xunit",
            "--no-build -noshadow -xml ../artifacts/report.xml -html ../artifacts/report.html -configuration " + configuration);

        /*var setting =  new DotNetCoreTestSettings{
                Configuration = configuration,
                ResultsDirectory = artifactsDirectory,
                NoBuild = true
            };
            DotNetCoreTest(testsProject.FullPath, setting);
     */
        /*XUnit2(testsDllPath, 
            new XUnit2Settings
            {
                HtmlReport = true,
                OutputDirectory = artifactsDirectory
            });*/
    });

Task("Package-Results")
    .IsDependentOn("Run-Tests")
    .Does (() => {
        var resultFile = GetFiles ("./artifacts/report.xml").First ().FullPath;
        var parser = new XunitXmlResultParser.XParser ("IFS FSMx Test Results", resultFile, "./artifacts/test_results.html");
        parser.Parse();
        //var results =  GetFiles("./artifacts/*.html");
        //CopyFiles(results, @"\\mkenas21\metrix$\RnD\Mobile\Osprey\build_results\");
    });

Task ("Prepare-Build")
    .IsDependentOn ("Clean")
    .IsDependentOn ("Restore-Packages")
    .Does (() => { });

Task ("Build-Android")
    .IsDependentOn ("Prepare-Build")
    .Does (() => {
        MSBuild (androidProject, settings =>
            settings.SetConfiguration (configuration)
            .WithProperty ("DebugSymbols", "false")
            .WithProperty ("TreatWarningsAsErrors", "false")
            .WithTarget ("SignAndroidPackage")
            .SetVerbosity (Verbosity.Minimal));
    });

Task ("Build-iOS")
    .IsDependentOn ("Prepare-Build")
    .Does (() => {
        MSBuild (iOSProject, settings =>
            settings.SetConfiguration (configuration)
            .WithProperty ("Platform", "iPhoneSimulator")
            .WithProperty ("OutputPath", $"bin/iPhoneSimulator/{configuration}/")
            .WithProperty ("DebugSymbols", "false")
            .WithProperty ("TreatWarningsAsErrors", "false")
            .SetVerbosity (Verbosity.Minimal));
    });

Task ("Build-Solution")
    .IsDependentOn ("Build-Android")
    .IsDependentOn ("Build-iOS")
    .Does (() => { });

Task ("Default")
    .IsDependentOn ("Build-Solution")
    .IsDependentOn ("Run-Tests")
    .IsDependentOn ("Package-Results");

RunTarget (target);

1 个答案:

答案 0 :(得分:1)

我已经可以使用xUnit和以下内容运行测试。我还没有解决的问题是在目标运行程序(iOS或Android)中运行它。但这确实执行了我的.NET Core单元测试。

DotNetCoreTest(UnitTestProjectPath, new DotNetCoreTestSettings
{ 
    Configuration = "<Build Configuration>" 
});