C# 从代码运行xunit时如何设置testCase筛选器

C# 从代码运行xunit时如何设置testCase筛选器,c#,xunit,C#,Xunit,我试图通过代码使用程序集运行程序在Xunit中运行测试 我的程序接受包含一组测试的dll文件名 我需要从dll文件运行一些特定的测试。我不是在看Xunit的类别实现。一旦发现完成,就需要接受测试用例 public List<ITestCase> TestCases { get; } = new List<ITestCase>(); 但是DiscoveryCompleteInfo只包含TestCasesToRun和TestCasesDiscovered的int值 如何为测

我试图通过代码使用程序集运行程序在Xunit中运行测试

我的程序接受包含一组测试的dll文件名

我需要从dll文件运行一些特定的测试。我不是在看Xunit的类别实现。一旦发现完成,就需要接受测试用例

public List<ITestCase> TestCases { get; } = new List<ITestCase>();
但是
DiscoveryCompleteInfo
只包含
TestCasesToRun
TestCasesDiscovered
的int值

如何为测试应用过滤器,以便在调用
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);
}
公共IList ExecuteTest(字符串自动测试套件) { 尝试 { _logger.LogInformation(“在{0}中开始测试”,AutomationTestSuite); IEnumerable assembly=GetReferenceGassemblies(自动测试套件); Assembly\u Assembly=Assembly.Where(s=>s.FullName.Contains(AutomationTestSuites)).FirstOrDefault(); 使用(var runner=AssemblyRunner.WithoutAppDomain(_assembly.Location)) { runner.OnDiscoveryComplete=OnDiscoveryComplete; runner.OnExecutionComplete=OnExecutionComplete; runner.OnTestFailed=OnTestFailed; runner.OnTestSkipped=OnTestSkipped; runner.OnTestPassed=OnTestPassed; _logger.登录信息(“发现测试”); //如果parallel设置为True,则并行运行Xunit运行程序 //如果最大并行线程数设置为-1,则Xunit Runner的线程数没有限制 runner.Start(parallel:true,maxParallelThreads:-1); 完成。WaitOne(); 完成。处理(); } 返回(测试响应); } 捕获(例外情况除外) { _LogError(“ExecuteTestFunctionality中的Exeption:”,ex);返回(testResponses); } } 公共静态IEnumerable GetReferenceGassemblies(字符串assemblyName) { var assemblies=新列表(); var dependencies=DependencyContext.Default.RuntimeLibraries; foreach(依赖项中的var库) { if(IsCandidateLibrary(library,assemblyName)) { var assembly=assembly.Load(新的AssemblyName(library.Name)); 组件。添加(组件); } } 返回组件; } 私有静态bool IsCandidateLibrary(RuntimeLibrary库,字符串assemblyName) { 返回库。名称==assemblyName ||library.Dependencies.Any(d=>d.Name.StartsWith(assemblyName)); } 私有void OnDiscoveryComplete(发现完整信息) { _logger.LogInformation($“正在运行{info.TestCasesDiscovered}测试的{info.TestCasesToRun}”); } 私有void OneExecutionComplete(ExecutionCompleteInfo信息) { _logger.LogInformation($“已完成:{Math.Round(info.ExecutionTime,executionTimeRoundOff)}s中的{info.TestsFailed}测试({info.TestsFailed}失败,{info.TestsSkipped}跳过)”; 完成。Set(); } 私有void OnTestFailed(TestFailedInfo信息) { _LogError(“测试[失败]{0}:{1}”,info.TestDisplayName,info.ExceptionMessage); } 私有void OnTestPassed(TestPassedInfo) { _logger.LogInformation(“测试[通过]:{0}”,info.MethodName); } 私有void OnTestSkipped(TestSkippedInfo info) { _LogWarning(“测试[跳过]{0}:{1}”,info.MethodName,info.skiperson); }
我需要从dll中筛选测试用例,并只运行选定的测试。运行程序包含一个
TestCaseFilter
属性

runner.TestCaseFilter = this.Filter;
//
///筛选指定的测试用例。
/// 
///测试用例。
///如果应该执行测试用例,则为true,否则为false。
受保护的布尔过滤器(ITestCase测试用例)
{
if(testCase.TestMethod.TestClass.Class.Name.StartsWith(“DoNotTest”))
{
返回false;
}
返回true;
}
您还可以使用该方法跟踪所有发现的测试用例

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;
    }