简体   繁体   English

如何在 XUNIT 中运行所有测试之前和之后运行方法,同时保持固定夹具?

[英]How can I run a a method prior and after all tests are run in XUNIT, while Keeping a fixed Fixture?

I am trying to implement a BeforeAfterMethodRun Though I have already a Fixture that was not written by me and is being used in other projects.我正在尝试实现BeforeAfterMethodRun虽然我已经有一个不是我编写的 Fixture 并且正在其他项目中使用。 Is there a way how I could add this while having another base class in my class?有没有办法在我的 class 中有另一个基础 class 时添加它? I get the following error:我收到以下错误:

在此处输入图像描述

My goal is to Execute the command prior to any of the methods in the class is called once, which can be as well called on every method call.我的目标是在 class 中的任何方法被调用一次之前执行命令,这也可以在每个方法调用上调用。 Though these methods would not be the same and would change from class to class.虽然这些方法不会相同,并且会从 class 更改为 class。

Is there a way to achieve this?有没有办法做到这一点?

Ok So I did not understand the Attribute decoration very well.好的所以我对属性装饰不是很了解。 I could achieve this by creating a new class as:我可以通过创建一个新的 class 来实现这一点:

using System;
using System.Reflection;
using Linedata.Amp.Qa.Foundation.Logger;
using Xunit.Sdk;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
class ReportBeforeAttribute : BeforeAfterTestAttribute
{
    public override void Before(MethodInfo methodUnderTest)
    {
        Trace.Log("Starting Method");
    }

    public override void After(MethodInfo methodUnderTest)
    {
        Trace.Log("MethodFinished ");
    }
}

And then Decorating the needed class as [ReportBefore]然后将所需的 class 装饰为[ReportBefore]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发现了XUnit测试,但未通过“运行所有测试”运行 - XUnit test is discovered but not run by 'run all tests' 在 xUnit.net 中的所有测试之前和之后运行一次代码 - Run code once before and after ALL tests in xUnit.net Xunit在一个appdomain中运行所有测试 - Xunit run all tests in one appdomain Xunit 单元测试不会运行 - Xunit Unit Tests will not run XUnit 测试的运行设置 - Run Settings for XUnit tests xUnit 是否支持“相同的测试,不同的设置”? 或者我如何编写在本地为开发人员运行的测试,并在管道中测试部署? - Does xUnit support "same tests, different setups"? Or how can I write tests that run locally for dev, and in a pipeline to test deployment? 如何解决XUnit测试失败的情况,在“全部运行”下的NLog目标中断言消息 - How to troubleshoot the situation where XUnit tests fail with assertion of messages in NLog target under “Run All” XUnit 测试可以在要测试的项目中运行吗? - Can XUnit tests be run from within the project to be tested? 如何针对net45的类库(包)项目运行xunit测试? - How do I run xunit tests for a Class Library (Package) project targeting net45? 如何在xUnit中执行全局设置/拆卸并并行运行测试? - How to perform global setup/teardown in xUnit and run tests in parallel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM