def test_error_message(self): """ Check that the appropriate error messages are printed on failure. """ stream = TestWrapperIO() failing_feature = 'features/wrong_expectations.feature' self.assert_feature_fail(failing_feature, '-n', '1', stream=stream) output = stream.getvalue() error_header = "FAIL: Fail at adding " + \ "(features.wrong_expectations: Wrong expectations)" self.assertIn(error_header, output) feature_stack_frame = """ File "{feature}", line 11, in Fail at adding Then the result should be 40 on the screen """.strip().format(feature=os.path.abspath(failing_feature)) self.assertIn(feature_stack_frame, output) step_file = self.step_module_filename('features.steps') step_stack_frame = """ File "{step_file}", line 62, in assert_result assert world.result == float(result) AssertionError """.strip().format(step_file=step_file) self.assertIn(step_stack_frame, output)
def test_color_output(self): """Test streamed output with color""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal), \ patch('aloe.result.AloeTestResult.printSummary'): self.run_features('features/highlighting.feature', verbosity=3, stream=stream, force_color=True) self.assertEqual(stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works t.grey(# features/highlighting.feature:15) t.green(Given I have a table) t.green(Given I have entered 10 into the calculator) t.green(And I press [+]) Scenario Outline: Scenario outlines t.grey(# features/highlighting.feature:19) | number | | 30 | t.green(Given I have a table) t.green(Given I have entered 30 into the calculator) t.green(And I press add) Scenario Outline: Scenario outlines t.grey(# features/highlighting.feature:19) | number | | 40 | t.green(Given I have a table) t.green(Given I have entered 40 into the calculator) t.green(And I press add) t.cyan(@tables) Scenario: Scenario with table t.grey(# features/highlighting.feature:29) t.green(Given I have a table) t.green(Given I have a table:) | t.green(value) | | t.green(1) | | t.green(1) | | t.green(2) | Scenario: Scenario with a multiline t.grey(# features/highlighting.feature:36) t.green(Given I have a table) t.green(Given I have a table:) \"\"\" t.green(Not actually a table :-P) \"\"\" """.lstrip())
def test_customized_color_output(self): """Test streamed color output with overridden colors.""" stream = TestWrapperIO() with self.environment_override('CUCUMBER_COLORS', 'failed=magenta:passed=blue'): with \ patch('aloe.result.Terminal', new=MockTerminal), \ patch('aloe.result.AloeTestResult.printSummary'): self.run_features('features/highlighting.feature', verbosity=3, stream=stream, force_color=True) self.assertEqual(stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read Scenario: behave_as works t.grey(# features/highlighting.feature:13) t.blue(Given I have a table) t.blue(Given I have entered 10 into the calculator) t.blue(And I press [+]) Scenario Outline: Scenario outlines t.grey(# features/highlighting.feature:17) | number | | 30 | t.blue(Given I have a table) t.blue(Given I have entered 30 into the calculator) t.blue(And I press add) Scenario Outline: Scenario outlines t.grey(# features/highlighting.feature:17) | number | | 40 | t.blue(Given I have a table) t.blue(Given I have entered 40 into the calculator) t.blue(And I press add) t.cyan(@tables) Scenario: Scenario with table t.grey(# features/highlighting.feature:27) t.blue(Given I have a table) t.blue(Given I have a table:) | t.blue(value) | | t.blue(1) | | t.blue(1) | | t.blue(2) | Scenario: Scenario with a multiline t.grey(# features/highlighting.feature:34) t.blue(Given I have a table) t.blue(Given I have a table:) \"\"\" t.blue(Not actually a table :-P) \"\"\" """.lstrip())
def test_color_output(self): """Test streamed output with color""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal), \ patch('aloe.result.AloeTestResult.printSummary'): self.run_features(self.feature, verbosity=3, stream=stream, force_color=True) self.assertEqual(stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works t.grey(# {feature}:15) t.green(Given I have a table) t.green(Given I have entered 10 into the calculator) t.green(And I press [+]) Scenario Outline: Scenario outlines t.grey(# {feature}:19) | number | | 30 | t.green(Given I have a table) t.green(Given I have entered 30 into the calculator) t.green(And I press add) Scenario Outline: Scenario outlines t.grey(# {feature}:19) | number | | 40 | t.green(Given I have a table) t.green(Given I have entered 40 into the calculator) t.green(And I press add) t.cyan(@tables) Scenario: Scenario with table t.grey(# {feature}:29) t.green(Given I have a table) t.green(Given I have a table:) | t.green(value) | | t.green(1) | | t.green(1) | | t.green(2) | Scenario: Scenario with a multiline t.grey(# {feature}:36) t.green(Given I have a table) t.green(Given I have a table:) \"\"\" t.green(Not actually a table :-P) \"\"\" """.lstrip().format(feature=self.feature))
def test_color_output(self): """Test streamed output with color""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal), \ patch('aloe.result.AloeTestResult.printSummary'): self.run_features('features/highlighting.feature', verbosity=3, stream=stream, force_color=True) self.assertEqual(stream.getvalue(), """ t.bold_white(Feature: Highlighting) t.white(As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read) t.bold_white(Scenario: behave_as works)t.color8(features/highlighting.feature:13) t.bold_green(Given I have a table) t.bold_green(Given I have entered 10 into the calculator) t.bold_green(And I press [+]) t.bold_white(Scenario Outline: Scenario outlines)t.color8(features/highlighting.feature:17) | t.white(number) | | t.white(30) | t.bold_green(Given I have a table) t.bold_green(Given I have entered 30 into the calculator) t.bold_green(And I press add) t.bold_white(Scenario Outline: Scenario outlines)t.color8(features/highlighting.feature:17) | t.white(number) | | t.white(40) | t.bold_green(Given I have a table) t.bold_green(Given I have entered 40 into the calculator) t.bold_green(And I press add) t.cyan(@tables) t.bold_white(Scenario: Scenario with table)t.color8(features/highlighting.feature:27) t.bold_green(Given I have a table) t.bold_green(Given I have a table:) | t.bold_green(value) | | t.bold_green(1) | | t.bold_green(1) | | t.bold_green(2) | t.bold_white(Scenario: Scenario with a multiline)t.color8(features/highlighting.feature:34) t.bold_green(Given I have a table) t.bold_green(Given I have a table:) \"\"\" t.bold_green(Not actually a table :-P) \"\"\" """.lstrip()) # noqa
def test_uncolored_output(self): """Test streamed output""" stream = TestWrapperIO() with patch('aloe.result.AloeTestResult.printSummary'): self.run_features(self.feature, verbosity=3, stream=stream) self.assertEqual( stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works # {feature}:15 Given I have a table Given I have entered 10 into the calculator And I press [+] Scenario Outline: Scenario outlines # {feature}:19 | number | | 30 | Given I have a table Given I have entered 30 into the calculator And I press add Scenario Outline: Scenario outlines # {feature}:19 | number | | 40 | Given I have a table Given I have entered 40 into the calculator And I press add @tables Scenario: Scenario with table # {feature}:29 Given I have a table Given I have a table: | value | | 1 | | 1 | | 2 | Scenario: Scenario with a multiline # {feature}:36 Given I have a table Given I have a table: \"\"\" Not actually a table :-P \"\"\" """.lstrip().format(feature=self.feature))
def test_uncolored_output(self): """Test streamed output""" stream = TestWrapperIO() with patch('aloe.result.AloeTestResult.printSummary'): self.run_features(self.feature, verbosity=3, stream=stream) self.assertEqual(stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works # {feature}:15 Given I have a table Given I have entered 10 into the calculator And I press [+] Scenario Outline: Scenario outlines # {feature}:19 | number | | 30 | Given I have a table Given I have entered 30 into the calculator And I press add Scenario Outline: Scenario outlines # {feature}:19 | number | | 40 | Given I have a table Given I have entered 40 into the calculator And I press add @tables Scenario: Scenario with table # {feature}:29 Given I have a table Given I have a table: | value | | 1 | | 1 | | 2 | Scenario: Scenario with a multiline # {feature}:36 Given I have a table Given I have a table: \"\"\" Not actually a table :-P \"\"\" """.lstrip().format(feature=self.feature))
def test_failure_zh(self): """ Test that a failing feature in Chinese fails tests. """ stream = TestWrapperIO() failing_feature = 'features/wrong_expectations_zh.feature' self.assert_feature_fail(failing_feature, stream=stream) # Check that the appropriate error messages were printed output = stream.getvalue() error_header = "FAIL: 添加两个数值 " + \ "(features.wrong_expectations_zh: 不对的预期)" self.assertIn(error_header, output) if PY3: feature_stack_frame = """ File "{feature}", line 12, in 添加两个数值 那么结果应该是40 """.strip().format(feature=os.path.abspath(failing_feature)) self.assertIn(feature_stack_frame, output) else: # Cannot use non-ASCII method names in Python 2 feature_stack_frame = """ File "{feature}", line 12, in """.strip().format(feature=os.path.abspath(failing_feature)) self.assertIn(feature_stack_frame, output) feature_code_line = "那么结果应该是40" self.assertIn(feature_code_line, output) step_file = self.step_module_filename('features.steps') step_stack_frame = """ File "{step_file}", line 62, in assert_result assert world.result == float(result) AssertionError """.strip().format(step_file=step_file) self.assertIn(step_stack_frame, output)
def test_step_not_found_zh(self): """ Check the behavior when a step is not defined with a Chinese feature. """ stream = TestWrapperIO() failing_feature = 'features/step_not_found_zh.feature' self.assert_feature_fail(failing_feature, stream=stream) output = stream.getvalue() error_header = "NoDefinitionFound: " + \ "The step r\"当我开曲速引擎\" is not defined" self.assertIn(error_header, output)
def test_step_not_found(self): """ Check the behavior when a step is not defined. """ stream = TestWrapperIO() failing_feature = 'features/step_not_found.feature' self.assert_feature_fail(failing_feature, stream=stream) output = stream.getvalue() error_header = "NoDefinitionFound: " + \ "The step r\"When I engage the warp drive\" is not defined" self.assertIn(error_header, output)
def test_error_message_outline(self): """ Check that the appropriate error messages are printed on failure in a scenario outline. """ stream = TestWrapperIO() failing_feature = 'features/wrong_expectations.feature' self.assert_feature_fail(failing_feature, '-n', '2', stream=stream) output = stream.getvalue() error_header = "FAIL: Fail repeatedly " + \ "(features.wrong_expectations: Wrong expectations)" self.assertIn(error_header, output) # TODO: Why isn't the line indented with 6 spaces like in the file? feature_stack_frame = """ File "{feature}", line 22, in Fail repeatedly: Example 1 | 50 | """.strip().format(feature=os.path.abspath(failing_feature)) self.assertIn(feature_stack_frame, output) example_stack_frame = """ File "{feature}", line 18, in Fail repeatedly Then the result should be <result> on the screen """.strip().format(feature=os.path.abspath(failing_feature)) self.assertIn(example_stack_frame, output) step_file = self.step_module_filename('features.steps') step_stack_frame = """ File "{step_file}", line 62, in assert_result assert world.result == float(result) AssertionError """.strip().format(step_file=step_file) self.assertIn(step_stack_frame, output)
def test_tty_output(self): """Test streamed output with tty control codes""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal) as mock_term, \ patch('aloe.result.AloeTestResult.printSummary'): mock_term.is_a_tty = True self.run_features('-n', '1', self.feature, verbosity=3, stream=stream) # we are going to see the scenario written out 3 times # once as a preview, then each line individually followed by a # green version of it self.assertEqual( stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works t.grey(# {feature}:15) t.grey(Given I have a table Given I have entered 10 into the calculator And I press [+]) <t.move_up><t.move_up><t.move_up>t.yellow(Given I have a table) <t.move_up>t.green(Given I have a table) t.yellow(Given I have entered 10 into the calculator) <t.move_up>t.green(Given I have entered 10 into the calculator) t.yellow(And I press [+]) <t.move_up>t.green(And I press [+]) """.lstrip().format(feature=self.feature))
def test_tty_output(self): """Test streamed output with tty control codes""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal) as mock_term, \ patch('aloe.result.AloeTestResult.printSummary'): mock_term.is_a_tty = True self.run_features('-n', '1', self.feature, verbosity=3, stream=stream) # we are going to see the scenario written out 3 times # once as a preview, then each line individually followed by a # green version of it self.assertEqual(stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works t.grey(# {feature}:15) t.grey(Given I have a table Given I have entered 10 into the calculator And I press [+]) <t.move_up><t.move_up><t.move_up>t.yellow(Given I have a table) <t.move_up>t.green(Given I have a table) t.yellow(Given I have entered 10 into the calculator) <t.move_up>t.green(Given I have entered 10 into the calculator) t.yellow(And I press [+]) <t.move_up>t.green(And I press [+]) """.lstrip().format(feature=self.feature))
def test_tty_output(self): """Test streamed output with tty control codes""" stream = TestWrapperIO() with \ patch('aloe.result.Terminal', new=MockTerminal) as mock_term, \ patch('aloe.result.AloeTestResult.printSummary'): mock_term.is_a_tty = True self.run_features('-n', '1', 'features/highlighting.feature', verbosity=3, stream=stream) # we are going to see the scenario written out 3 times # once in color 8 as a preview, then each line individually # followed by a green version of it self.assertEqual(stream.getvalue(), """ t.bold_white(Feature: Highlighting) t.white(As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read) t.bold_white(Scenario: behave_as works)t.color8(features/highlighting.feature:13) t.color8(Given I have a table Given I have entered 10 into the calculator And I press [+]) <t.move_up><t.move_up><t.move_up>t.color11(Given I have a table) <t.move_up>t.bold_green(Given I have a table) t.color11(Given I have entered 10 into the calculator) <t.move_up>t.bold_green(Given I have entered 10 into the calculator) t.color11(And I press [+]) <t.move_up>t.bold_green(And I press [+]) """.lstrip()) # noqa
def run_features(self, *features, **kwargs): """ Run the specified features. """ # named keyword args and variable positional args aren't supported on # Python 2 verbosity = kwargs.get('verbosity') stream = kwargs.get('stream') force_color = kwargs.get('force_color', False) if stream is None and isinstance(sys.stdout, CAPTURED_OUTPUTS): # Don't show results of running the inner tests if the outer Nose # redirects output stream = TestWrapperIO() # Reset the state of callbacks and steps so that individual tests don't # affect each other CALLBACK_REGISTRY.clear(priority_class=PriorityClass.USER) STEP_REGISTRY.clear() world.__dict__.clear() argv = ['aloe'] if verbosity: argv += ['--verbosity', str(verbosity)] if force_color: argv += ['--color'] argv += list(features) # Save the loaded module list to restore later old_modules = set(sys.modules.keys()) result = TestRunner(exit=False, argv=argv, stream=stream) result.captured_stream = stream # To avoid affecting the (outer) testsuite and its subsequent tests, # unload all modules that were newly loaded. This also ensures that they # are loaded again for the next tests, registering relevant steps and # hooks. new_modules = set(sys.modules.keys()) for module_name in new_modules - old_modules: del sys.modules[module_name] return result
def test_customized_color_output(self): """Test streamed color output with overridden colors.""" stream = TestWrapperIO() with self.environment_override('CUCUMBER_COLORS', 'failed=magenta:passed=blue'): with \ patch('aloe.result.Terminal', new=MockTerminal), \ patch('aloe.result.AloeTestResult.printSummary'): self.run_features(self.feature, verbosity=3, stream=stream, force_color=True) self.assertEqual( stream.getvalue(), """ Feature: Highlighting As a programmer I want to see my scenarios with pretty highlighting So my output is easy to read 非ASCII字显示得正常 Scenario: behave_as works t.grey(# {feature}:15) t.blue(Given I have a table) t.blue(Given I have entered 10 into the calculator) t.blue(And I press [+]) Scenario Outline: Scenario outlines t.grey(# {feature}:19) | number | | 30 | t.blue(Given I have a table) t.blue(Given I have entered 30 into the calculator) t.blue(And I press add) Scenario Outline: Scenario outlines t.grey(# {feature}:19) | number | | 40 | t.blue(Given I have a table) t.blue(Given I have entered 40 into the calculator) t.blue(And I press add) t.cyan(@tables) Scenario: Scenario with table t.grey(# {feature}:29) t.blue(Given I have a table) t.blue(Given I have a table:) | t.blue(value) | | t.blue(1) | | t.blue(1) | | t.blue(2) | Scenario: Scenario with a multiline t.grey(# {feature}:36) t.blue(Given I have a table) t.blue(Given I have a table:) \"\"\" t.blue(Not actually a table :-P) \"\"\" """.lstrip().format(feature=self.feature))