首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用vstest.console.exe工具运行Selenium测试时显示已测试URL

使用vstest.console.exe工具运行Selenium测试时显示已测试URL
EN

Stack Overflow用户
提问于 2016-07-27 13:44:08
回答 1查看 527关注 0票数 2

上下文

我配置了Jenkins作业,它执行Selenium C#测试。在构建作业时,我必须提供两个价值:

  • 分支来构建我的解决方案
  • 应该测试的URL

详细描述

Jenkins执行以下步骤:

  • 结帐选择分支
  • 将AppSettings中存储的URL替换为用户提供的URL ("TestedURL")
  • 构建解决方案
  • 执行11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow")工具( Visual 2012附带,在以下路径中存在:"Microsoft vstest.console.exe,如下所示: vstest.console.exe "c:\myProject\bin\Debug\myProject.Test.dll“

问题

我希望确保正确的URL得到测试:我希望在Jenkins/vstest.sole e.exe输出的控制台输出中显示URL。

我在StackOverflow上按照不同的答案修改了代码。

URL可直接从Visual的测试输出中看到。不幸的是,我仍然没有在vstest.sole e.exe/Jenkins输出中看到任何测试URL。

如何在输出中显示URL (如何修改printMessage方法)?

我的代码如下:

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-08-04 17:13:35

因为我找不到直接的解决办法,所以我找到了解决办法。

vstest.console.exe有/logger:trx选项,它生成具有.trx扩展名的文件。上述文件是一个xml,它包含测试结果和Std输出。我注意到,当我在测试方法中使用我的打印消息时,消息是存在的(在trx文件中的Std输出中)。

让我提供部分生成的.trx文件:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
# 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 $outputHTMLFile
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38614724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档