Debian Codegraph @

features/steps/behave_context_steps.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 Step definition for Context object tests.
4
5 EXAMPLE
6 Scenario: Show that Context parameter
7 Given I set the parameter "person" to "Alice" in the behave context
8 Then the behave context should have a parameter named "person"
9 And the behave context object should contain:
10 | Parameter | Value |
11 | person | "Alice" |
12
13 Scenario: Show that Context parameter are not present in next scenario
14 Then the behave context should not have a parameter named "person"
15 """
16
17 from __future__ import absolute_import
18 from behave import given, then, step
19 from hamcrest import assert_that, equal_to
20 import six
21
22 # -----------------------------------------------------------------------------
23 # STEPS:
24 # -----------------------------------------------------------------------------
25 @step(u'I set the context parameter "{param_name}" to "{value}"')
26 def step_set_behave_context_parameter_to(context, param_name, value):
27 setattr(context, param_name, value)
28
29 @step(u'the parameter "{param_name}" exists in the behave context')
30 def step_behave_context_parameter_exists(context, param_name):
31 assert hasattr(context, param_name)
32
33 @step(u'the parameter "{param_name}" does not exist in the behave context')
34 def step_behave_context_parameter_not_exists(context, param_name):
35 assert not hasattr(context, param_name)
36
37 @given(u'the behave context has a parameter "{param_name}"')
38 def given_behave_context_has_parameter_named(context, param_name):
39 step_behave_context_parameter_exists(context, param_name)
40
41 @given(u'the behave context does not have a parameter "{param_name}"')
42 def given_behave_context_does_not_have_parameter_named(context, param_name):
43 step_behave_context_parameter_not_exists(context, param_name)
44
45 @step(u'the behave context should have a parameter "{param_name}"')
46 def step_behave_context_should_have_parameter_named(context, param_name):
47 step_behave_context_parameter_exists(context, param_name)
48
49 @step(u'the behave context should not have a parameter "{param_name}"')
50 def step_behave_context_should_not_have_parameter_named(context, param_name):
51 step_behave_context_parameter_not_exists(context, param_name)
52
53 @then(u'the behave context should contain')
54 def then_behave_context_should_contain_with_table(context):
55 assert context.table, "ENSURE: table is provided."
56 for row in context.table.rows:
57 param_name = row["Parameter"]
58 param_value = row["Value"]
59 if param_value.startswith('"') and param_value.endswith('"'):
60 param_value = param_value[1:-1]
61 actual = six.text_type(getattr(context, param_name, None))
62 assert hasattr(context, param_name)
63 assert_that(actual, equal_to(param_value))
64
65 @given(u'the behave context contains')
66 def given_behave_context_contains_with_table(context):
67 then_behave_context_should_contain_with_table(context)