从代码运行xunit时如何设置testCase过滤器

时间:2019-04-22 07:15:14

标签: c# xunit

我正在尝试使用汇编运行器通过代码在Xunit中运行测试。我的程序接受包含一组测试的dll文件名。我需要从dll文件中运行一些特定的测试。 xunit的类别实现。发现完成后,需要接受测试用例 。    public List<ITestCase> TestCases { get; } = new List<ITestCase>();在此处输入代码

如何将不同的测试用例添加到列表中。

我希望我们需要在调用之后进行过滤

   private void OnDiscoveryComplete(DiscoveryCompleteInfo info)
  {
 }

但是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);
        }

需要从dll过滤测试用例并仅运行选定的测试

1 个答案:

答案 0 :(得分:2)

赛跑者包含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;
    }

您还可以使用该方法来跟踪所有发现的测试用例。