实现行为驱动测试的nose2插件。

planterbox的Python项目详细描述


https://api.travis-ci.org/npilon/planterbox.png?branch=master

用于运行行为驱动测试的插件 gherkin内部 nose2

用法

要为项目启用planterbox,您需要添加 跟随unittest.cfg的行(或类似行):

[unittest]plugins=planterbox[planterbox]always-on=True

planterboxnose2.plugins.mp兼容。

编写测试

Python包中存在p>^ {tt2}测试,它提供了 执行它们的上下文。您应该在^{tt7}中编写测试$ 包目录中的文件。.feature文件可以访问所有 在其包的__init__.py中定义或导入的步骤。为 例如,目录结构:

planterbox/
  tests/
    test_feature/
      __init__.py
      basic.feature

如果__init__.py包含:

fromplanterboximportstep@step(r'I add (\d+) and (\d+)')defadd(test,a,b):a=int(a)b=int(b)test.result=a+b@step(r'the result should be (\d+)')defcheck_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 1Then the result should be 2

我们可以通过运行 使用nose2设置,或使用 nose2 planterbox.tests.test_feature:basic.feature。我们甚至可以跑 第一个场景 nose2 planterbox.tests.test_feature:basic.feature:0

写入步骤

planterboxsteps是用 @planterbox.step(PATTERN)PATTERN可以是python正则 表达式,它必须在 gherkin step prefixes

PATTERN内匹配的组被提供给修饰函数 作为论据。所有步骤都随^{tt19}提供$ 对象作为其第一个参数。 未命名的组作为位置参数提供给步骤 这个。命名组将作为关键字参数传递。PATTERN 无法混合未命名组和命名组。如果使用任何命名组,则所有 组必须是命名组。

功能包中的所有步骤都可用于该功能的 脚本。这些步骤可以在包中定义,也可以从 在别的地方。

挂钩、设置和拆卸

^特征中的{tt21}$和tearDownModule方法 __init__.py将在其中所有功能之前和之后运行 分别打包。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

您的'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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java ClientServersocket只能发送1个文件   使用jCUDA实现复杂矩阵的java操作   映射是否存在独立于持久性技术的Java数据API   java Android图像在ExiFinInterface不工作的情况下旋转   java api通过串口发送文件   java使用Restcontroller将空值转换为空字符串   如何在Java中指定网络接口   在一个不工作的安卓系统中,java在另一个文本视图的右边膨胀文本视图   Java JSON解析器错误   java单空间Swing文本   我在eclipse中制作的java游戏在导出后停止加载我的资产   java将给定字符串转换为long,并在解码时返回相同的字符串   java在安卓应用程序中使用dll库   javascript映射json对象并创建随机数组   java JMS主题生存时间   在Swing中的JTextArea上使用setText时出现java死锁   java JFXtra议程是否只在特定时间之间显示?