为nose2实现行为驱动测试的插件。
项目描述
一个在nose2中使用gherkin运行行为驱动测试的插件。
使用方法
要为您的项目启用planterbox,您需要将以下行(或类似内容)添加到您的unittest.cfg
[unittest]
plugins = planterbox
[planterbox]
always-on = True
planterbox 与 nose2.plugins.mp 兼容。
编写测试
planterbox 测试位于一个提供执行上下文的Python包中。您应该在包目录中的.feature文件中编写您的测试。 .feature文件可以访问它们包的__init__.py中定义或导入的所有步骤。例如,在以下目录结构中
planterbox/ tests/ test_feature/ __init__.py basic.feature
如果__init__.py包含
from planterbox import step
@step(r'I add (\d+) and (\d+)')
def add(test, a, b):
a = int(a)
b = int(b)
test.result = a + b
@step(r'the result should be (\d+)')
def check_result(test, value):
value = int(value)
test.assertEqual(test.result, value)
basic.feature可以包含
Feature: Basic Tests
I want to exercise generation of a simple test from a feature.
Scenario: I need to verify basic arithmetic.
Given I add 1 and 1
Then the result should be 2
然后我们可以通过运行套件中的所有测试或使用nose2 planterbox.tests.test_feature:basic.feature具体运行此测试。我们甚至可以具体运行第一个场景,使用nose2 planterbox.tests.test_feature:basic.feature:0。
编写步骤
planterbox 的步骤是使用 @planterbox.step(PATTERN) 装饰的 Python 函数。 PATTERN 可以是一个 Python 正则表达式,它必须在 gherkin 步骤前缀 之后匹配预期的步骤文本。
在 PATTERN 中匹配的组将作为参数传递给装饰函数。所有步骤都将提供当前执行场景的 ScenarioTestCase 对象作为其第一个参数。未命名的组将在此之后作为位置参数传递给步骤。命名组将作为关键字参数传递。 PATTERN 不能混合未命名的和命名组。如果使用了任何命名组,则所有组都必须是命名组。
一个功能包中的所有步骤都将对该功能的场景可用。这些步骤可以在包中定义或从其他地方导入。
钩子、设置和清理
功能 __init__.py 中的 setUpModule 和 tearDownModule 方法将分别在每个包中的所有功能之前和之后运行。 planterbox 提供了一些额外的钩子用于准备或清理。可以通过用 @planterbox.hook(TIMING, STAGE) 装饰函数来注册钩子。 TIMING 可以是 'before' 或 'after',而 STAGE 可以是 'feature'、'scenario'、'step'、'failure' 或 'error' 中的任何一个。
所有钩子都期望接受一个参数:活动功能的 TestCase 子类。
与步骤一样,钩子必须直接存在于功能的 __init__.py 中才能运行。
场景概述
planterbox 支持场景概述。这允许您使用不同的值多次执行场景。例如
Feature: Example Tests
I want to exercise generation of a test with examples from a feature.
Scenario Outline: I need to verify basic arithmetic with examples.
Given I add <x> and <y>
Then the result should be <z>
Examples:
x | y | z
1 | 1 | 2
1 | 2 | 3
2 | 1 | 3
2 | 2 | 4
您还可以通过使用 Examples file: 后跟下一行上 .csv 文件的路径来在 .csv 文件中指定示例。
Feature: Example Tests
I want to exercise using a csv file for examples
Scenario Outline: I want to exercise using a csv file for examples
Given I squiggly-add {<x>} and {<y>}
Then the result should be <z>
Examples file:
/tests/test_feature/examples.csv
您的 'before' 和 'after' 'scenario' 钩子将只在整个场景概述中运行一次。
调用测试
您可以通过允许 nose2 的自动发现找到所有测试来运行测试,或者您可以在命令行中指定要运行的特定测试。当指定特定测试时,您可以指定整个包、整个功能或单个场景。单个场景可以通过索引(从 0 开始)或名称指定。
nose2 planterbox.tests.test_feature
nose2 planterbox.tests.test_feature:basic.feature planterbox.tests.test_hooks:hooks.feature
nose2 planterbox.tests.test_feature:basic.feature:1
nose2 planterbox.tests.test_feature:basic.feature:0
nose2 planterbox.tests.test_feature:basic.feature:"I need to verify basic arithmetic"
nose2 planterbox.tests.test_feature:basic.feature:"I need to verify basic arithmetic."
如果您的功能包含具有相同名称的多个场景,则在给出该名称时将运行所有场景。带有尾随点的名称可以指定带或不带尾随点。
验证测试
您可以使用 --planterbox-check-only 标志来验证您的功能是否正确,而不运行它们
nose2 --planterbox-check-only planterbox.tests.test_feature ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK
失败将产生跟踪输出
nose2 --planterbox-check-only planterbox.tests.test_feature Traceback (most recent call last): File "planterbox_ve/bin/nose2", line 11, in <module> sys.exit(discover()) File "nose2/main.py", line 306, in discover return main(*args, **kwargs) File "nose2/main.py", line 100, in __init__ super(PluggableTestProgram, self).__init__(**kw) File "/opt/python/lib/python3.6/unittest/main.py", line 94, in __init__ self.parseArgs(argv) File "nose2/main.py", line 133, in parseArgs self.createTests() File "nose2/main.py", line 258, in createTests self.testNames, self.module) File "nose2/loader.py", line 68, in loadTestsFromNames for name in event.names] File "nose2/loader.py", line 68, in <listcomp> for name in event.names] File "nose2/loader.py", line 83, in loadTestsFromName result = self.session.hooks.loadTestsFromName(event) File "nose2/events.py", line 225, in __call__ result = getattr(plugin, self.method)(event) File "nose2/plugins/loader/discovery.py", line 247, in loadTestsFromName return Discoverer.loadTestsFromName(self, event) File "nose2/plugins/loader/discovery.py", line 84, in loadTestsFromName self._find_tests_in_module(event, module, top_level_dir)) File "nose2/plugins/loader/discovery.py", line 229, in _find_tests_in_module event, full_path, top_level_dir): File "nose2/plugins/loader/discovery.py", line 161, in _find_tests_in_dir event, path, entry_path, top_level): File "nose2/plugins/loader/discovery.py", line 176, in _find_tests_in_file result = self.session.hooks.handleFile(evt) File "nose2/events.py", line 225, in __call__ result = getattr(plugin, self.method)(event) File "planterbox/planterbox/plugin.py", line 106, in handleFile feature_path=feature_path, File "planterbox/planterbox/plugin.py", line 75, in makeSuiteFromFeature config=self.config, File "planterbox/planterbox/feature.py", line 81, in __init__ self.check_scenarios() File "planterbox/planterbox/feature.py", line 227, in check_scenarios raise UnmatchedStepException("Unmatched steps:\n" + '\n'.join(unmatched)) planterbox.exceptions.UnmatchedStepException: Unmatched steps: Given I bad 1 and 1
项目详细信息
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。