规范 - 如何在方案中使用多个表的组合

时间:2018-03-08 18:58:09

标签: c# .net bdd specflow

我正在使用Specflow执行BDD测试。我想在多个浏览器上测试我的导航菜单。特别是确保按钮在浏览器中显示。我不想在每个浏览器上专门为每个菜单项创建一堆测试,我不想创建一个覆盖每个浏览器/菜单项组合的大表。有没有办法指定2个表,然后创建一个执行两者组合的方案?

例如:

菜单项表

| menuItem |
| Home     |
| About    |
| Contact  |

浏览器表

| browser |
| Chrome  |
| Firefox |
| IE      |

方案

Scenario Outline: I can see menu item
    Given I navigate to the "/" page using <browser>
    Then I can see the menu item <menuItem>

预期的结果是,当它运行时,它将运行9次测试。

2 个答案:

答案 0 :(得分:1)

我个人更喜欢招募所有组合。可能会发生一个或两个不需要,或者您需要为每个等指定特殊的期望值:

Scenario: Flat scenario
    Given I have the following config:
    | menuItem | browser |
    | Home     | Chrome  |
   #| Home     | IE      | - ok, this is not needed
    | Home     | Firefox |
    | About    | Chrome  |
    | About    | IE      |
    | About    | Firefox |
    | Contact  | Chrome  |
    | Contact  | IE      |
    | Contact  | Firefox |
    Then something happens

如果你真的想要创建完整的组合,我会使用Scenario Outline和示例:

Scenario Outline: Combined scenario
    Given I have the following config:
    | MenuItem | Browser   |
    | Home     | <browser> |
    | About    | <browser> |
    | Contact  | <browser> |
    Then something happens

    Examples:
    | browser |
    | Chrome  |
    | Firefox |
    | IE      |

<强>更新

在基础方法中,最后一个参数可以是Table。因此,在上面的示例中,您可以按如下方式获取表格:

[Given(@"I have the following config:")]
public void InitFromConfiguration(Table table)
{
    // now the table has MenuItem and Browser columns
}

考虑到浏览器对于一个测试用例是不变的,我会按如下方式更改它:

Scenario Outline: Even better combined scenario
    Given I have the following items in the specified <browser>:
    | MenuItem |
    | Home     |
    | About    |
    | Contact  |
    When I test the browser with the given menuItems
    Then I have no errors

    Examples:
    | browser |
    | Chrome  |
    | Firefox |
    | IE      |

[Given(@"I have the following items in the specified (.*):")]
public void InitFromConfiguration(string browser, Table menuItems)
{
    // now the browser comes from the Examples and menuItems has 3 rows
}

如果您更喜欢强类型而不是Table,您甚至可以为菜单项定义转换步骤:

[Binding]
public class MyTransformations
{
    [StepArgumentTransformation]
    public MenuItem[] ToMenuItems(Table table)
    {
        return table.Rows.Select(row => new MenuItem(row[0])).ToArray();
    }        
}

现在您可以按如下方式定义Given

[Given(@"I have the following items in the specified (.*):")]
public void InitFromConfiguration(string browser, MenuItem[] menuItems)
{
    ScenarioContext.Current.Set(browser, nameof(browser));
    ScenarioContext.Current.Set(menuItems, nameof(menuItems));
}

When步骤中进行测试。这就是你将如何调用DoTest 9次:

[When(@"I test the (.*) with the given (.*)")]
public void InitFromConfiguration(string browserKey, string menuItemsKey)
{
    var browser = ScenarioContext.Current.Get<string>(browserKey);
    var menuItems = ScenarioContext.Current.Get<MenuItem[]>(menuItemsKey);

    // TODO: in DoTest you can collect and save the possible errors in the context
    foreach (MenuItem mi in menuItems)
        DoTest(browser, mi);
}

最后在Then步骤中,您可以断言您收集的可能错误并将其存储到DoTest方法的上下文中。

答案 1 :(得分:0)

您要写的测试:

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Form } from '../form';

@Component({
  selector: 'app-invoices',
  templateUrl: './invoices.component.html',
  styleUrls: ['./invoices.component.scss']
})
export class InvoicesComponent extends FormBuilder {

  constructor() { 
    super()

    this.createForm();
  }
}

无论如何,您应该瞄准跨浏览器的相同功能,因此只需为每个浏览器运行整套测试。

在配置文件中,指定要运行的浏览器以及构建驱动程序时,从配置中检查要运行的浏览器。