Jenkins 上的 NUnit 测试失败(nunit xml 结果文件为空)

NUnit Test on Jenkins fail (nunit xml result file is empty )

我最近开始在 Jenkins 构建服务器上构建我的 C# 项目。

但最近我遇到了问题,报告的 NUnit xml 是空的(文件已创建但没有内容。)

控制台输出如下

Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information [WARNINGS] Parsing warnings in console log with parser MSBuild [WARNINGS] Computing warning deltas based on reference build #288 Recording NUnit tests results ERROR: Step ‘Publish NUnit test result report’ aborted due to exception: hudson.util.IOException2: Could not transform the NUnit report. Please report this issue to the plugin author at hudson.plugins.nunit.NUnitArchiver.invoke(NUnitArchiver.java:65) at hudson.plugins.nunit.NUnitArchiver.invoke(NUnitArchiver.java:26) at hudson.FilePath.act(FilePath.java:990) at hudson.FilePath.act(FilePath.java:968) at hudson.plugins.nunit.NUnitPublisher.perform(NUnitPublisher.java:145) at hudson.tasks.BuildStepMonitor.perform(BuildStepMonitor.java:20) at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782) at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:723) at hudson.model.Build$BuildExecution.post2(Build.java:185) at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:668

我知道问题可能出在泄露的文件描述符上,但我不确定如何解决。

NUnit 测试使用 powershell 脚本执行,该脚本获取所有必要的 dll

powershell 脚本:

param(
[string] $sourceDirectory = "./trunk/TestProjects/"
, $fileFilters = @("*UnitTest*.dll")
, [string]$filterText = "*\bin*"
)

#script that executes all unit tests available.


Write-Host "Source: $sourceDirectory"
Write-Host "File Filters: $fileFilters"
Write-Host "Filter Text: $filterText"

$cFiles = ""
$nUnitExecutable = "C:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe"

# look through all subdirectories of the source folder and get any unit test assemblies. To avoid duplicates, only use the assemblies in the bin folder
[array]$files = get-childitem $sourceDirectory -include $fileFilters -recurse | select -expand FullName | where {$_ -like $filterText}

foreach ($file in $files)
{
    $cFiles = $cFiles + '"' + $file + '"' + " "
}

# set all arguments and execute the unit console
$argumentList = @("$cFiles", "--result=nunit-result.xml;format=nunit2","--framework=net-4.5","--process=Single")

$unitTestProcess = start-process -filepath $nUnitExecutable -argumentlist $argumentList -nonewwindow
echo "$nUnitExecutable $argumentList"


$exitCode = $unitTestProcess.ExitCode

exit $exitCode

只有通过jenkins执行脚本才会出现这个问题

#######################更新

经过一番调查后,我发现只有当我添加 1 个测试用例时才会发生这种情况,其中通过在 UI 线程上调用它们来创建 wpf 控件。

        [Test Apartment(ApartmentState.STA) RunInApplicationDomain]
    public void CheckPluginModel()
    {
        var app = Application.Current ?? new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
        PluginModel model = new PluginModel();

        var task= model.LoadPluginsFromPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));


        RunApplication(() => task.IsCompleted);


        Assert.That(model.AvailablePluginControls.Count, Is.EqualTo(1));
        Assert.That(model.Workflows.Count, Is.EqualTo(1));
        Console.WriteLine("Plugin model check finished");

    }

运行应用程序:

        /// <summary>
    /// Runs the application. as long as abortCriteria returns false
    /// </summary>
    /// <param name="abortCriteria">The abort criteria.</param>
    private void RunApplication(Func<bool> abortCriteria, int duetime = 100, int period=100)
    {

        Console.WriteLine("Application started");
        System.Threading.Timer timer = null;
        timer = new Timer((obj) =>
        {
            if (abortCriteria.Invoke())
            {
                Application.Current.Dispatcher.Invoke(() => Application.Current.Shutdown());
                timer.Dispose();

            }
        }, null, duetime, period); 

        Application.Current.Run();

        Console.WriteLine("Application stopped");
    }

创建了所有 GUI 个元素

await Application.Current.Dispatcher.BeginInvoke(new Action(() => AvailablePluginControls.Add((APControl)Activator.CreateInstance(item))),null);

Jenkins 中的 Nunit 插件不支持 Nunit 3 xml 格式。我在 Jenkins 上也有类似的问题。我曾经将 Nunit 3 结果格式转换为 Nunit 2 格式。

"C:\NUnit 3.5.0\nunit3-console.exe" /result:\MyApplication.xml;format=nunit2 "D:\Jenkins\workspace\MyApplication.Tests.dll"

问题如下:

单元测试通过 powershell 脚本执行。 powershell 启动过程不会等到过程完成。从而导致子进程未完成的问题。

为了启动进程等待进程完成,必须添加标志 -Wait