0

I am starting with Python behave and got stuck when trying to access context - it's not available. Here is my code:

Here is the Feature file:

Feature: Company staff inventory
    Scenario: count company staff
            Given a set of employees:
                    | name | dept |
                    | Paul | IT   |
                    | Mary | IT   |
                    | Pete | Dev  |

    When we count the number of employees in each department
    Then we will find two people in IT
    And we will find one employee in Dev

Here is the Steps file:

from behave import *
@given('a set of employees')
def step_impl(context):
        assert context is True
@when('we count the number of employees in each department')
def step_impl(context):
        context.res = dict()
        for row in context.table:
                for k, v in row:
                        if k not in context.res:
                                context.res[k] = 1
                        else:
                                context.res[k] += 1
@then('we will find two people in IT')
def step_impl(context):
        assert context.res['IT'] == 2
@then('we will find one employee in Dev')
def step_impl(context):
        assert context.res['Dev'] == 1

Here's the Traceback:

      Traceback (most recent call last):
        File "/home/kseniyab/Documents/broadsign_code/spikes/BDD_Gherkin/behave/src/py3behave/lib/python3.4/site-packages/behave/model.py", line 1456, in run
          match.run(runner.context)
        File "/home/kseniyab/Documents/broadsign_code/spikes/BDD_Gherkin/behave/src/py3behave/lib/python3.4/site-packages/behave/model.py", line 1903, in run
          self.func(context, *args, **kwargs)
        File "steps/count_staff.py", line 5, in step_impl
          assert context is True
      AssertionError
4

1 回答 1

0

context对象在那里您的断言有问题:

assert context is True

失败是因为context is not True。唯一能满足x is True的就是 wherex已经设置为Truecontext尚未设置为True。它是一个对象。请注意,测试是否context is True与测试是否要编写if context:真正的分支或错误的分支是不同的。后者相当于测试是否bool(context) is True

context测试是否可用是没有意义的。它总是在那里。

于 2016-01-20T17:34:02.907 回答