上下文
我配置了Jenkins作业,它执行Selenium C#测试。在构建作业时,我必须提供两个价值:
详细描述
Jenkins执行以下步骤:
问题
我希望确保正确的URL得到测试:我希望在Jenkins/vstest.sole e.exe输出的控制台输出中显示URL。
我在StackOverflow上按照不同的答案修改了代码。
URL可直接从Visual的测试输出中看到。不幸的是,我仍然没有在vstest.sole e.exe/Jenkins输出中看到任何测试URL。
如何在输出中显示URL (如何修改printMessage方法)?
我的代码如下:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Configuration;
using System.Diagnostics;
namespace My.Test
{
[TestClass]
public class MyTests
{
IWebDriver driver;
string baseURL;
[TestInitialize]
public void Initialize()
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// URL is replaced by the value provided in Jenkins
baseURL = config.AppSettings.Settings["TestedURL"].Value;
driver = new ChromeDriver();
driver.Navigate().GoToUrl(baseURL);
printMessage(string.Format("Tested URL: '{0}'", baseURL));
}
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private void printMessage(string message)
{
// Method should display custom message in both Visual Studio output and Jenkins (vstest.console.exe) output
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.Listeners.Add(new ConsoleTraceListener());
Trace.WriteLine(message);
Debug.WriteLine(message);
TestContext.WriteLine(message);
Console.WriteLine(message);
}
}
}发布于 2016-08-04 17:13:35
因为我找不到直接的解决办法,所以我找到了解决办法。
vstest.console.exe有/logger:trx选项,它生成具有.trx扩展名的文件。上述文件是一个xml,它包含测试结果和Std输出。我注意到,当我在测试方法中使用我的打印消息时,消息是存在的(在trx文件中的Std输出中)。
让我提供部分生成的.trx文件:
<Output>
<StdOut>Tested URL was 'http://someURL'
Debug Trace:
Tested URL: 'http://someURL'
Trying to log into as 'Incorrect account'
TestContext Messages:
Tested URL: 'http://someURL'
Trying to log into as 'Incorrect account'</StdOut>
</Output>
</UnitTestResult>有一些工具允许将.trx转换为.html。因此Jenkins可以在控制台中显示到测试结果的输出链接。
但
我有问题,因为我找不到正确工作的工具,并在生成的html文件中显示Std输出。从那以后,我编写了下面的Powershell代码:
# set $inputTRXFile, $outputHTMLFile before running following lines
$results = ([xml] (Get-Content $inputTRXFile)).TestRun.Results.UnitTestResult
$results | select testname,outcome,duration, @{Name="Output";Expression={$_.Output.InnerText}} | ConvertTo-HTML -head $a -title "Test results" | Out-File $outputHTMLFilehttps://stackoverflow.com/questions/38614724
复制相似问题