Multiple Multi-Line Examples in SpecFlow Feature File

我们两清 提交于 2019-12-11 06:18:41

问题


This seems like one of those questions that, if you know the answer, it's obvious, and if you don't, it's impossible...

How do I include a table of multi-line examples in a SpecFlow feature file?

My example would be:

        Given there is some invalid input:
        | input |
        | """ Multi-line example 1
              because there are multiple lines
              """ |
        | """Multi-line example 2
             with even more lines
             than the previous example
             """" |
    When something interesting happens
    Then the error is shown

Thanks in advance.


回答1:


you can do it like this:

Given there is some invalid input:
    | <Here goes column Name> | <Column Name2..>  |
    | Line 1 for column 1     | Line 1 for column2|
    | Line 2 for column 1     | Line 2 for column2|
    | ..and so on             | and so on...      |
When something interesting happens
Then the error is shown

and this will be translate to

[Given(@"there is some invalid input:")]
public void GivenThereIsSomeInvalidInput(Table table)
{
   foreach (var row in table.Rows)
   {
       string info1= = row["<Here goes column Name>"];
       string info2= = row["<Column Name2..>"];
   }
}

and I understand you have a couple of sets of invalid input, well you could make another scenario, exactly like this, only add more input data in the table, no extra code needed.

Hope this solves your problem




回答2:


Well, it looks like this isn't possible according to a poster in the Google SpecFlow group. He also points out that I may have too much implementation in my behavior test, and maybe this fits into unit tests more appropriately.




回答3:


Since I do the comparison of actual and expected values myself (not using the automatic table comparison feature of SpecFlow), I allow using regular expressions for special values, like strings containing newline:

Then I expect the result values
    | Name            | Value              |
    | Multilinestring | @@Multline\nString |

And my compare function does this:

private static bool compare (string actual, string expected)
{
    if (expected.StartsWith("@@"))
        return Regex.Match(actual, expected.Substring(2)).Success;
    ....
}


来源:https://stackoverflow.com/questions/6191178/multiple-multi-line-examples-in-specflow-feature-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!