Debian Codegraph @

features/formatter.user_defined.feature

raw
1 Feature: Use a user-defined Formatter
2
3 As a behave user
4 I want to be able to provide own, user-defined formatters
5 So that I am able to add a new, user-defined formatters
6 when I need other output formats (or override existing ones).
7
8 . SPECIFICATION:
9 . * A user-defined formatter must inherit from the "Formatter" class.
10 . * A user-defined formatter can be specified on command-line
11 . by using a scoped class name as value for the '--format' option.
12 . * A user-defined formatter can be registered by name
13 . by using the "behave.formatters" section in the behave config file.
14 .
15 . SCOPED CLASS NAME (for formatter.class):
16 . * my.module_name:ClassName (preferred: single colon separator)
17 . * my.module_name::ClassName (alternative: double colon separator)
18
19
20 @setup
21 Scenario: Feature Setup
22 Given a new working directory
23 And a file named "features/steps/passing_steps.py" with:
24 """
25 from behave import step
26
27 @step('{word:w} step passes')
28 def step_passes(context, word):
29 pass
30
31 @step('{word:w} step fails')
32 def step_fails(context, word):
33 assert False, "XFAIL-STEP"
34 """
35 And a file named "features/passing.feature" with:
36 """
37 Feature:
38 Scenario: Alice
39 Given a step passes
40 When another step passes
41
42 Scenario: Bob
43 Then some step passes
44 """
45 And an empty file named "behave_ext/__init__.py"
46 And a file named "behave_ext/formatter_one.py" with:
47 """
48 from behave.formatter.base import Formatter
49
50 class NotAFormatter(object): pass
51 class SuperFormatter(Formatter):
52 name = "super"
53 description = "Super-duper formatter."
54 """
55 And a file named "behave_ext/formatter_foo.py" with:
56 """
57 from behave.formatter.base import Formatter
58
59 class FooFormatter(Formatter):
60 name = "foo"
61 description = "User-specific FOO formatter."
62
63 class FooFormatter2(Formatter):
64 description = "User-specific FOO2 formatter."
65 """
66 And a file named "behave_ext/formatter_bar.py" with:
67 """
68 from behave.formatter.base import Formatter
69
70 class BarFormatter(Formatter):
71 description = "User-specific BAR formatter."
72 """
73
74 @use_formatter.class
75 Scenario: Use a known, valid user-defined formatter with scoped class name
76 When I run "behave -f behave_ext.formatter_one:SuperFormatter features/passing.feature"
77 Then it should pass with:
78 """
79 2 scenarios passed, 0 failed, 0 skipped
80 3 steps passed, 0 failed, 0 skipped, 0 undefined
81 """
82
83 @use_formatter.class
84 Scenario: Use a known, valid user-defined formatter (with double colon)
85
86 ALTERNATIVE: Can currently use a double colon as separator, too.
87
88 When I run "behave -f behave_ext.formatter_one::SuperFormatter features/passing.feature"
89 Then it should pass with:
90 """
91 2 scenarios passed, 0 failed, 0 skipped
92 3 steps passed, 0 failed, 0 skipped, 0 undefined
93 """
94
95
96 @use_formatter.class
97 Scenario Outline: Use built-in formatter "<formatter.name>" like a user-defined formatter
98 When I run "behave -f <formatter.class> features/passing.feature"
99 Then it should pass with:
100 """
101 2 scenarios passed, 0 failed, 0 skipped
102 3 steps passed, 0 failed, 0 skipped, 0 undefined
103 """
104
105 Examples:
106 | formatter.name | formatter.class |
107 | plain | behave.formatter.plain:PlainFormatter |
108 | pretty | behave.formatter.pretty:PrettyFormatter |
109
110
111 @problematic
112 @use_formatter.class
113 Scenario Outline: Use a problematic user-defined formatter (<case>)
114 When I run "behave -f <formatter.class> features/passing.feature"
115 Then it should fail with:
116 """
117 error: format=<formatter.class> is unknown
118 """
119
120 Examples:
121 | formatter.class | case |
122 | my.unknown_module:SomeFormatter | Unknown module |
123 | behave_ext.formatter_one:UnknownClass | Unknown class |
124 | behave_ext.formatter_one:NotAFormatter | Invalid Formatter class |
125
126
127 @formatter.registered_by_name
128 Scenario Outline: Register user-defined formatter by name: <formatter.name>
129 Given a file named "behave.ini" with:
130 """
131 [behave.formatters]
132 foo = behave_ext.formatter_foo:FooFormatter
133 foo2 = behave_ext.formatter_foo:FooFormatter2
134 bar = behave_ext.formatter_bar:BarFormatter
135 """
136 And note that "the schema: 'formatter.name = formatter.class' is used"
137 When I run "behave -f <formatter.name> features/passing.feature"
138 Then it should pass with:
139 """
140 2 scenarios passed, 0 failed, 0 skipped
141 3 steps passed, 0 failed, 0 skipped, 0 undefined
142 """
143 But the command output should not contain:
144 """
145 error: format=<formatter.name> is unknown
146 """
147
148 Examples:
149 | formatter.name | Comment |
150 | foo | First user-defined, registered formatter. |
151 | bar | Last user-defined, registered formatter. |
152
153
154 @formatter.registered_by_name
155 Scenario: Help-Format shows description of registered-by-name formatters
156 Given a file named "behave.ini" with:
157 """
158 [behave.formatters]
159 foo = behave_ext.formatter_foo:FooFormatter
160 bar = behave_ext.formatter_bar:BarFormatter
161 """
162 When I run "behave -f help"
163 Then it should pass with:
164 """
165 Available formatters:
166 """
167 And the command output should contain:
168 """
169 bar User-specific BAR formatter.
170 """
171 And the command output should contain:
172 """
173 foo User-specific FOO formatter.
174 """
175
176
177 @problematic
178 @formatter.registered_by_name
179 Scenario Outline: Use problematic, registered-by-name formatter: <case>
180 Given a file named "behave.ini" with:
181 """
182 [behave.formatters]
183 <formatter.name> = <formatter.class>
184 """
185 When I run "behave -f <formatter.name> features/passing.feature"
186 Then it should fail with:
187 """
188 error: format=<formatter.name> is unknown
189 """
190
191 Examples:
192 | formatter.name | formatter.class | case |
193 | unknown1 | my.unknown_module:SomeFormatter | Unknown module |
194 | unknown2 | behave_ext.formatter_one:UnknownClass | Unknown class |
195 | invalid1 | behave_ext.formatter_one:NotAFormatter | Invalid Formatter class |
196