1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
"""Test code generation command."""
import os
import sys
import textwrap
from pytest_bdd.scripts import main
PATH = os.path.dirname(__file__)
def test_generate(pytester, monkeypatch, capsys):
"""Test if the code is generated by a given feature."""
features = pytester.mkdir("scripts")
feature = features.joinpath("generate.feature")
feature.write_text(
textwrap.dedent(
"""\
Feature: Code generation
Scenario: Given and when using the same fixture should not evaluate it twice
Given I have an empty list
And 1 have a fixture (appends 1 to a list) in reuse syntax
When I use this fixture
Then my list should be [1]
"""
),
"utf-8",
)
monkeypatch.setattr(sys, "argv", ["", "generate", str(feature)])
main()
out, err = capsys.readouterr()
assert out == textwrap.dedent(
'''\
"""Code generation feature tests."""
from pytest_bdd import (
given,
scenario,
then,
when,
)
@scenario('scripts/generate.feature', 'Given and when using the same fixture should not evaluate it twice')
def test_given_and_when_using_the_same_fixture_should_not_evaluate_it_twice():
"""Given and when using the same fixture should not evaluate it twice."""
@given('1 have a fixture (appends 1 to a list) in reuse syntax')
def _():
"""1 have a fixture (appends 1 to a list) in reuse syntax."""
raise NotImplementedError
@given('I have an empty list')
def _():
"""I have an empty list."""
raise NotImplementedError
@when('I use this fixture')
def _():
"""I use this fixture."""
raise NotImplementedError
@then('my list should be [1]')
def _():
"""my list should be [1]."""
raise NotImplementedError
'''
)
def test_generate_with_quotes(pytester):
"""Test that code generation escapes quote characters properly."""
pytester.makefile(
".feature",
generate_with_quotes=textwrap.dedent(
'''\
Feature: Handling quotes in code generation
Scenario: A step definition with quotes should be escaped as needed
Given I have a fixture with 'single' quotes
And I have a fixture with "double" quotes
And I have a fixture with single-quote \'\'\'triple\'\'\' quotes
And I have a fixture with double-quote """triple""" quotes
When I generate the code
Then The generated string should be written
'''
),
)
result = pytester.run("pytest-bdd", "generate", "generate_with_quotes.feature")
assert result.stdout.str() == textwrap.dedent(
'''\
"""Handling quotes in code generation feature tests."""
from pytest_bdd import (
given,
scenario,
then,
when,
)
@scenario('generate_with_quotes.feature', 'A step definition with quotes should be escaped as needed')
def test_a_step_definition_with_quotes_should_be_escaped_as_needed():
"""A step definition with quotes should be escaped as needed."""
@given('I have a fixture with "double" quotes')
def _():
"""I have a fixture with "double" quotes."""
raise NotImplementedError
@given('I have a fixture with \\'single\\' quotes')
def _():
"""I have a fixture with 'single' quotes."""
raise NotImplementedError
@given('I have a fixture with double-quote """triple""" quotes')
def _():
"""I have a fixture with double-quote \\"\\"\\"triple\\"\\"\\" quotes."""
raise NotImplementedError
@given('I have a fixture with single-quote \\'\\'\\'triple\\'\\'\\' quotes')
def _():
"""I have a fixture with single-quote \'\'\'triple\'\'\' quotes."""
raise NotImplementedError
@when('I generate the code')
def _():
"""I generate the code."""
raise NotImplementedError
@then('The generated string should be written')
def _():
"""The generated string should be written."""
raise NotImplementedError
'''
)
def test_unicode_characters(pytester, monkeypatch):
"""Test generating code with unicode characters.
Primary purpose is to ensure compatibility with Python2.
"""
pytester.makefile(
".feature",
unicode_characters=textwrap.dedent(
"""\
Feature: Generating unicode characters
Scenario: Calculating the circumference of a circle
Given We have a circle
When We want to know its circumference
Then We calculate 2 * ℼ * 𝑟
"""
),
)
result = pytester.run("pytest-bdd", "generate", "unicode_characters.feature")
expected_output = textwrap.dedent(
'''\
"""Generating unicode characters feature tests."""
from pytest_bdd import (
given,
scenario,
then,
when,
)
@scenario('unicode_characters.feature', 'Calculating the circumference of a circle')
def test_calculating_the_circumference_of_a_circle():
"""Calculating the circumference of a circle."""
@given('We have a circle')
def _():
"""We have a circle."""
raise NotImplementedError
@when('We want to know its circumference')
def _():
"""We want to know its circumference."""
raise NotImplementedError
@then('We calculate 2 * ℼ * 𝑟')
def _():
"""We calculate 2 * ℼ * 𝑟."""
raise NotImplementedError
'''
)
assert result.stdout.str() == expected_output
|