c# - 从代码运行xunit时如何设置testCase过滤器
问题描述
我正在尝试通过代码使用程序集运行器在 Xunit 中运行测试。
我的程序接受包含一组测试的 dll 文件名。
我需要从 dll 文件中运行一些特定的测试。我不是在看 Xunit 的类别实现。发现完成后需要接受测试用例。
public List<ITestCase> TestCases { get; } = new List<ITestCase>();
如何将不同的测试用例添加到列表中?
我希望我们需要在调用后进行过滤
private void OnDiscoveryComplete(DiscoveryCompleteInfo info) { }
但仅包含和DiscoveryCompleteInfo
的 int 值。TestCasesToRun
TestCasesDiscovered
如何为测试应用过滤器,以便在调用一次时根据过滤器执行测试OnDiscoveryComplete
?
public IList<TestResponse> ExecuteTest(string AutomationTestSuites)
{
try
{
_logger.LogInformation("Starting of the tests in {0} ", AutomationTestSuites);
IEnumerable<Assembly> assembly = GetReferencingAssemblies(AutomationTestSuites);
Assembly _assembly = assembly.Where(s => s.FullName.Contains(AutomationTestSuites)).FirstOrDefault();
using (var runner = AssemblyRunner.WithoutAppDomain(_assembly.Location))
{
runner.OnDiscoveryComplete = OnDiscoveryComplete;
runner.OnExecutionComplete = OnExecutionComplete;
runner.OnTestFailed = OnTestFailed;
runner.OnTestSkipped = OnTestSkipped;
runner.OnTestPassed = OnTestPassed;
_logger.LogInformation("Discovering Tests");
//Runs the Xunit runner in parallel if parallel is set to True
//If Max Parallel Threads is set to -1 there is no limit to number of threads for Xunit Runner
runner.Start(parallel: true, maxParallelThreads: -1);
finished.WaitOne();
finished.Dispose();
}
return (testResponses);
}
catch(Exception ex)
{
_logger.LogError("Exeption in ExecuteTestFunctionality : ", ex); return (testResponses);
}
}
public static IEnumerable<Assembly> GetReferencingAssemblies(string assemblyName)
{
var assemblies = new List<Assembly>();
var dependencies = DependencyContext.Default.RuntimeLibraries;
foreach (var library in dependencies)
{
if (IsCandidateLibrary(library, assemblyName))
{
var assembly = Assembly.Load(new AssemblyName(library.Name));
assemblies.Add(assembly);
}
}
return assemblies;
}
private static bool IsCandidateLibrary(RuntimeLibrary library, string assemblyName)
{
return library.Name == assemblyName
|| library.Dependencies.Any(d => d.Name.StartsWith(assemblyName));
}
private void OnDiscoveryComplete(DiscoveryCompleteInfo info)
{
_logger.LogInformation($"Running {info.TestCasesToRun} of {info.TestCasesDiscovered} tests...");
}
private void OnExecutionComplete(ExecutionCompleteInfo info)
{
_logger.LogInformation($"Finished: {info.TotalTests} tests in {Math.Round(info.ExecutionTime, executionTimeRoundOff)}s ({info.TestsFailed} failed, {info.TestsSkipped} skipped)");
finished.Set();
}
private void OnTestFailed(TestFailedInfo info)
{
_logger.LogError("Test [FAILED] {0}: {1}", info.TestDisplayName, info.ExceptionMessage);
}
private void OnTestPassed(TestPassedInfo info)
{
_logger.LogInformation("Test [Passed] : {0}", info.MethodName);
}
private void OnTestSkipped(TestSkippedInfo info)
{
_logger.LogWarning("Test [SKIPPED] {0}: {1}", info.MethodName,info.SkipReason);
}
我需要从 dll 中过滤测试用例并仅运行选定的测试
解决方案
跑步者包含一个TestCaseFilter
属性。
runner.TestCaseFilter = this.Filter;
/// <summary>
/// Filters the specified test case.
/// </summary>
/// <param name="testCase">The test case.</param>
/// <returns><c>true</c> if test case should be executed, <c>false</c> otherwise.</returns>
protected bool Filter(ITestCase testCase)
{
if(testCase.TestMethod.TestClass.Class.Name.StartsWith("DoNotTest"))
{
return false;
}
return true;
}
您还可以使用该方法跟踪所有发现的测试用例。
推荐阅读
- php - 通过所有文件夹将 CSS 文件与 PHP 链接
- javascript - 使用复选框获取产品 id 并使用 id 创建一个新数组
- python - 尝试实现 MERGE 算法合并到 python 中的排序整数列表时出错?
- subdomain - 仅限 LAN 上的 Caddy
- javascript - TypeError: _this7._config.server.rewriteRequestUrl 不是函数 React native 0.64.1
- laravel - cookie 未显示在其他路线中
- windows - Docker 设置和后端
- xml - 如何在 Android Studio 4.2 中使用新的 Basic Activity?
- javascript - 当触发它的页面关闭时,Javascript不会被触发
- omnet++ - 计算静脉中的 PSR 与 SNIR