【问题标题】:Issue with automating 'Then' step BDD C# Specflow问题自动化'然后'步骤bdd c#specflow
【发布时间】:2018-11-13 11:44:54
【问题描述】:

我正在运行一个测试,我正在使用我的预期输出“6677,6677_6677,3001,6”验证文件(例如 irm_xxx_lkdd_xuxt.csv.ovr)的输出

我遇到的问题是我的规范流“然后”步骤无法识别下面的代码。我认为问题可能是因为我正在使用 Nunit 测试用例。有办法解决这个问题吗?或者我可以在我的 ValidateMeasurement 方法中结合我的文件路径和预期结果

    [Then("Transfer measure should be generated for (.*)")]


[TestCase("irm_xxx_lkdd_xuxt.csv.ovr", "6677,6677_6677,3001,6")]
[TestCase("irm_xxx_lkdd_fcvt.csv.ovrr", "6677,6677_6677,3001,6")]
[TestCase("irm_xxx_lkdd_fbvt.csv.ovrr", "6677,6677_6677,3001,6")]

public void ValidateMeasurement(string path, string expected)
{
    const string processFilePath = "/orabin/app/product/inputs/ff/actuals/";
    var actual = Common.LinuxCommandExecutor
                       .RunLinuxcommand($"cat {processFilePath}{path}");

    Assert.AreEqual(expected, actual);

}



Given I Loaded LifeCycle Measurement for Current
And Inventory interface is generated 
When Inventory batch is executed
Then Transfer measure should be generated Current
Examples:
| Lifecyclestatus |
| PreNew          |
| New             |
| Current         |
| Clearance       |
| Old             |

【问题讨论】:

    标签: c# visual-studio testing bdd specflow


    【解决方案1】:

    不要混合使用 BDD 和 NUnit 测试用例。 Specflow 在后台生成 NUnit 测试,但这并不意味着您必须考虑 BDD,因为它与单元测试有任何关系。

    您的案例应该是Examples,因此它将在后台转换为测试用例 - 但对您而言,它应该是透明的,因为它可能是幕后的任何其他引擎。

    所以 - 在不知道任何进一步细节的情况下 - 我会这样做:

    Scenario Outline: My fantastic test with multiple cases
        Given I have a <Path>
        When I perform a test
        Then the expected result is <Expected>
    
    Examples:
    | Path                       | Expected              |
    | irm_xxx_lkdd_xuxt.csv.ovr  | 6677,6677_6677,3001,6 |
    | irm_xxx_lkdd_fcvt.csv.ovrr | 6677,6677_6677,3001,6 |
    | irm_xxx_lkdd_fbvt.csv.ovrr | 6677,6677_6677,3001,6 |
    

    Given 步骤中您可以存储任何配置(也许只是存储路径是一个过于简单的示例),When 步骤用于进行实际测试,最后在Then 步骤中进行断言。

    [Binding]
    public class MyFantasticFeatureBindings
    {
        [Given("I have a (.*)")]
        public void ConfigureTest(string path)
        {
            // setup any configuration here - actually it can be the expected value, too
            ScenarioContext.Current.Set(path, nameof(path));
        }
    
        [When("I perform a test")]
        public void DoTest()
        {
            // obtain configuration, do the test and store the results and possible errors
            var path = ScenarioContext.Current.Get<string>("path");
    
            var result = PerformTest(path); // TODO - you have to implement this
    
            ScenarioContext.Current.Set(result, nameof(result)); 
        }
    
        [Then("the expected result is (.*)")]
        public void Assertions(string expectedResult)
        {
            var actualResult = ScenarioContext.Current.Get<string>("result");
            Assert.AreEqual(expectedResult, actualResult);
        }
    }
    

    【讨论】:

    • 我已经用我的功能文件编辑了我的帖子。在我的例子中,我有不同的状态。因此,对于我的“然后”步骤,一旦从“时间”步骤执行批处理,将生成不同的度量。一旦生成了这些措施,我想“Cat”文件并将文件的输出与我的预期结果进行比较。
    • 好的,这意味着您毕竟需要 N x M 步。但这并没有改变答案的要点:您需要从功能文件中获取所有需要的数据,并且不要尝试将其与 NUnit 案例显式混合。也许我在这里的其他答案可以提供帮助:stackoverflow.com/a/49182246/5114784
    • 是的,我同意。但是没有办法在我的方法中组合文件路径和预期结果吗?所以基本上我仍然可以使用我在“然后”步骤中拥有的代码,从而处理文件路径并比较预期结果。
    • 是的,如果路径和预期结果都只在Then步骤中处理,这可能是一个合理的解决方案。
    • 是的,它们仅在“然后”步骤中处理。使用上面的示例,我如何在方法中集成文件路径和预期结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多