【问题标题】:Specflow beforeScenario in base class called multiple times, once for each feature in the suite基类中的 Specflow beforeScenario 调用多次,套件中的每个功能调用一次
【发布时间】:2020-01-29 15:07:42
【问题描述】:

我有一个使用测试报告框架的测试套件,我需要通过一个基类运行该框架,一切正常,但是对于我运行的每个场景,[BeforeScenario] 被调用 10 次,我的每个功能被调用 1 次套件,奇怪的是拆解只运行一次。

这是我的基类的样子:

[Binding]
public class BaseStep : AllureReport
{
    public DriverSupport _support;
    public BaseStep(DriverSupport support)
    {
        _support = support;
    }

    [BeforeScenario]
    public void Setup(FeatureContext context)
    {
            _support.startDriver();
            AllureLifecycle.Instance.RunStep(() =>
            {
                TestContext.Progress.WriteLine(
                    $"Test \"{TestExecutionContext.CurrentContext.CurrentTest.FullName}\" is starting...");
            });

    }

    [AfterScenario, Order(0)]
    private void TearDown()
    {

        AllureLifecycle.Instance.RunStep(() =>
        {
            TestContext.Progress.WriteLine(
                $"Test {TestExecutionContext.CurrentContext.CurrentTest.FullName}\" is stopping...");
        });

}

这就是我的功能步骤文件的样子(其中有 10 个)

[Binding]
public class TestSteps1:BaseStep
{

    public TestSteps1(DriverSupport support) : base(suppprt)
    {
    }

    [Given(@"user goes to (.*)")]
    public void GivenUserGoesTo(string p0)
    {
        _support.driver.GoToUrl(p0)
    }

当我自己在一个特性中运行一个场景时,它会打印启动驱动程序并打印“test xxx is starting...”10 次,对于我拥有的每个特性一次,我只希望它运行一次。

我曾考虑将 beforescenario 移至 step 类本身,但我的许多功能使用来自多个 step 文件的 step,所以我认为这将是一个问题。有没有办法让它在每个场景中只运行一次 BeforeScenario?

【问题讨论】:

    标签: c# specflow allure


    【解决方案1】:

    SpecFlow 将为它在您的测试项目中找到的每种类型执行 BeforeScenario 方法。看起来有 10 种类型定义或继承了这个方法:

    • BaseStep 类
    • BaseStep 的每个子类

    当我希望一个 BeforeScenario 在每次测试运行时只运行一次时,我会将它放入它自己的“钩子”类中。

    由于 before 和 after 钩子都应该只执行一次,只需将它们都放在 SpecFlowHooks.cs 之类的东西中。

    Selenium Web 驱动程序对象应注册到 SpecFlow 依赖注入框架,并作为构造函数参数直接传递给每个步骤类。鉴于您发布的代码,我不确定您的步骤定义的通用基类是否有益。

    有关使用 SpecFlow 正确注册 Selenium Web 驱动程序对象的更多信息,请参阅我对 How to properly manage and access webdriver instances to avoid problems with parallel execution of tests? 的回答。

    【讨论】:

    • 按照您的建议,我删除了我的基类并将其方法移至 hooks 类,我使用了我的 DriverSuport 类而不是容器的 IWebdriver,包括报告在内的所有内容都按现在应该的方式工作,谢谢!
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多