Ash*_*h K 3 c# mstest xunit azure-devops .net-5
我们有一些 UI 集成测试无法在构建服务器上运行,因为启动测试 GUI 应用程序需要以用户身份运行构建代理(而不是当前设置的服务)。
这会导致构建管道卡住。所以我想在本地运行这些测试,而不是在构建服务器上。
有没有办法使用xUnit或MSTests和Azure DevOps构建管道来实现此目的?
你当然可以。
设置环境变量以指示它是否在build.yml文件中的构建服务器上运行。
variables:
- name: IsRunningOnBuildServer
value: true
Run Code Online (Sandbox Code Playgroud)
现在创建一个自定义事实属性来使用它:
// This is taken from this SO answer: /sf/answers/309535901/
public class IgnoreOnBuildServerFactAttribute : FactAttribute
{
public IgnoreOnBuildServerFactAttribute()
{
if (IsRunningOnBuildServer())
{
Skip = "This integration test is skipped running in the build server as it involves launching an UI which requires build agents to be run as non-service. Run it locally!";
}
}
/// <summary>
/// Determine if the test is running on build server
/// </summary>
/// <returns>True if being executed in Build server, false otherwise.</returns>
public static bool IsRunningOnBuildServer()
{
return bool.TryParse(Environment.GetEnvironmentVariable("IsRunningOnBuildServer"), out var buildServerFlag) ? buildServerFlag : false;
}
}
Run Code Online (Sandbox Code Playgroud)
FactAttribute现在,在您想要跳过在构建服务器上运行的测试方法上使用它。例如:
[IgnoreOnBuildServerFact]
public async Task Can_Identify_Some_Behavior_Async()
{
// Your test code...
}
Run Code Online (Sandbox Code Playgroud)
创建自定义测试方法属性来覆盖该Execute方法:
public class SkipTestOnBuildServerAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
if (!IsRunningOnBuildServer())
{
return base.Execute(testMethod);
}
else
{
return new TestResult[] { new TestResult { Outcome = UnitTestOutcome.Inconclusive } };
}
}
public static bool IsRunningOnBuildServer()
{
return bool.TryParse(Environment.GetEnvironmentVariable("IsRunningOnBuildServer"), out var buildServerFlag) ? buildServerFlag : false;
}
}
Run Code Online (Sandbox Code Playgroud)
TestMethodAttribute现在,在您想要跳过在构建服务器上运行的测试方法上使用它。例如:
[SkipTestOnBuildServer]
public async Task Can_Identify_Some_Behavior_Async()
{
// Your test code...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3466 次 |
| 最近记录: |