如何在不删除功能的情况下禁用specflow(Gherkin)中的某个功能?

78

我有一些使用Gherkin语法编写的SpecFlow功能,我想暂时禁用该功能,以防止其测试运行。

是否有属性可用于标记该功能以实现此操作? 我猜想在Cucumber中起作用的某些东西也可以在SpecFlow中使用。

5个回答

113

您可以使用标签@ignore来标记该特性:

@ignore @web
Scenario: Title should be matched
When I perform a simple search on 'Domain'
Then the book list should exactly contain book 'Domain Driven Design'

1
酷,我可以在哪里找到属性列表? - Simon Keep
3
我们在谈论标签。@ignore 是唯一的“预定义”标签。其他标签可以自由定义,以控制 Before-/After-Hooks 和有选择地运行功能。 - jbandi
1
我会查看生成器的代码,因为它提供了最准确的答案: https://github.com/techtalk/SpecFlow/blob/master/Generator/UnitTestProvider/MsTest2010GeneratorProvider.cs 还有SpecFlow更改日志https://github.com/techtalk/SpecFlow/blob/master/changelog.txt,我会引用:“MsTest:支持MSTest的[Owner]和[WorkItem]属性,带有类似于\@owner:foo @workitem:123(Issue 162,Pull 161)的标签”。 不过,属性列表也有点取决于您使用的测试框架和测试运行程序以及SpecFlow :) - Borislav T
我发现在每个功能中的每个场景都需要添加 @ignore 属性。 - DanH
@DanH,你应该能够将整个功能标记为@ignore - nitzel
当我们在Azure管道中运行标记为@ignore的测试用例时会发生什么?该测试用例会被标记为失败还是甚至不会考虑在管道运行中? - Apoorv

19
在最新的Specflow版本中,您现在还需要在标签中提供原因,例如:
@ignore("reason for ignoring")

编辑:由于某种原因,它会在空格处中断,但这样做可以解决:

@ignore("reason")

3

正如jbandi所建议的那样,您可以使用@ignore标签。

该标签可应用于:

  • 单个场景
  • 整个功能

假设NUnit是测试提供程序,则生成的代码中的结果是将

[NUnit.Framework.IgnoreAttribute()]

插入到方法或类中。


1
Feature: CheckSample

@ignored
Scenario Outline: check ABC    #checkout.feature:2
Given I open the applciation
When I enter username as "<username>"
And I enter password as "<password>"
Then I enter title as "<title>"
Examples:
| username | password |
| dude     | daad     |

请将上述内容视为“CheckSample.feature”功能文件。

以下是您的步骤文件,仅为部分文件:

public class Sampletest {


@Given("^I open the applciation$")
public void i_open_the_applciation() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    //throw new PendingException();
}

现在下面将是运行文件:
@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "html:target/reports", 
"json:target/reports/cucumber-report.json"},
        monochrome = true,
        tags = {"~@ignored"}
        )

public class junittestOne {

   public static void main(String[] args) {
        JUnitCore junit = new JUnitCore();
         Result result = junit.run(junittestOne.class);
   }

  }

重要的是要注意,在功能文件中的"@ignored"文本在CucumberOptions(标签)中与"junittestone"类文件中提到。此外,请确保您的项目具有所有相关的jar文件,包括cucumber、gherkin、Junit和其他jar文件,并且您已将它们导入到步骤定义(类)中。
由于"ignored",在执行测试时,该场景将被跳过。

如果不写成﹫ignore而是写成﹫ignored,它就不会被忽略。抱歉,我不确定如何在这里写AT符号。 - hogan

0

如果你不想运行某个特性文件,可以在场景前面写上 @smoke。 然后,在 Runner 文件中,写上 tag = "not @smoke"。 这样就会运行所有的特性文件,但不包括你标注为 @smoke 的那一个。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接