def test_scenario_not_found(request): """Test the situation when scenario is not found.""" with pytest.raises(exceptions.ScenarioNotFound) as exc_info: scenario('not_found.feature', 'NOT FOUND') assert six.text_type(exc_info.value).startswith( 'Scenario "NOT FOUND" in feature "[Empty]" in {feature_path}'.format( feature_path=request.fspath.join('..', 'not_found.feature')))
def test_scenario_not_found(request): """Test the situation when scenario is not found.""" with pytest.raises(exceptions.ScenarioNotFound): scenario( 'not_found.feature', 'NOT FOUND' )
def test_verbose_output(request): """Test verbose output of failed feature scenario""" with pytest.raises(FeatureError) as excinfo: scenario('when_after_then.feature', 'When after then') msg, line_number, line = excinfo.value.args assert line_number == 5 assert line == 'When I do it again'
def test_scenario_not_found(request): """Test the situation when scenario is not found.""" with pytest.raises(exceptions.ScenarioNotFound) as exc_info: scenario( 'not_found.feature', 'NOT FOUND' ) assert six.text_type(exc_info.value).startswith( 'Scenario "NOT FOUND" in feature "[Empty]" in {feature_path}' .format(feature_path=request.fspath.join('..', 'not_found.feature')))
def test_verbose_output(): """Test verbose output of failed feature scenario.""" with pytest.raises(exceptions.FeatureError) as excinfo: scenario('when_after_then.feature', 'When after then') msg, line_number, line, file = excinfo.value.args assert line_number == 5 assert line == 'When I do it again' assert file == os.path.join(os.path.dirname(__file__), 'when_after_then.feature') assert line in str(excinfo.value)
def test_feature_path(request, scenario_name): """Test feature base dir.""" sc = scenario('steps.feature', scenario_name) with pytest.raises(IOError) as exc: sc(request) assert os.path.join('/does/not/exist/', 'steps.feature') in str(exc.value)
def test_multiple_given(request): """Using the same given fixture raises an error.""" test = scenario( 'args_steps.feature', 'Using the same given fixture raises an error', ) with pytest.raises(GivenAlreadyUsed): test(request)
def test_scenario_not_decorator(request): """Test scenario function is used not as decorator.""" func = scenario( 'comments.feature', 'Strings that are not comments') with pytest.raises(exceptions.ScenarioIsDecoratorOnly): func(request)
def test_scenario_not_found(request): """Test the situation when scenario is not found.""" test_not_found = scenario( 'not_found.feature', 'NOT FOUND' ) with pytest.raises(ScenarioNotFound): test_not_found(request)
def test_verbose_output(request): """Test verbose output of failed feature scenario""" sc = scenario('wrong.feature', 'When after then') with pytest.raises(FeatureError) as excinfo: sc(request) msg, line_number, line = excinfo.value.args assert line_number == 4 assert line == 'When I do it again'
def test_scenario_not_decorator(request): """Test scenario function is used not as decorator.""" func = scenario('comments.feature', 'Strings that are not comments') with pytest.raises(exceptions.ScenarioIsDecoratorOnly): func(request)
import pytest from pytest_bdd import scenario, given, when, then @pytest.fixture def pytestbdd_close_browser(): """Close browser fixture.""" return False test_successful_login = scenario( 'auth/admin_login.feature', 'Successful login', ) @given('I\'m an admin user') def admin_user(user): return user @when('I go to the admin login page') def go_to_login_page(browser): browser.visit('http://127.0.0.1:5000') @when('I fill in the login credentials') def fill_in_login_credentials(browser, user, password): browser.fill('username', user.username) browser.fill('password', password)
from pytest_bdd import scenario, given, when, then test_steps = scenario('steps.feature', 'Executed step by step') @given('I have a foo fixture with value "foo"') def foo(): return 'foo' @given('there is a list') def results(): return [] @when('I append 1 to the list') def append_1(results): results.append(1) @when('I append 2 to the list') def append_2(results): results.append(2) @when('I append 3 to the list') def append_3(results): results.append(3) @then('foo should have value "foo"')
import re from urlparse import urljoin from pytest_bdd import when, scenario test_sucsessful_login = scenario( "auth/login.feature", "Successful login", ) test_invalid_username = scenario( "auth/login.feature", "Invalid username", ) test_ivalid_password = scenario( "auth/login.feature", "Invalid password", ) @when("I go to login page") def go_to_login_page(browser): browser.visit(urljoin(browser.url, "/login")) @when(re.compile('I enter "(?P<username>\w+)" username')) def enter_username(browser, username): browser.fill("username", username)
"""Step arguments tests.""" import functools import re import pytest from pytest_bdd import scenario, given, when, then from pytest_bdd.scenario import GivenAlreadyUsed test_steps = scenario( 'args_steps.feature', 'Every step takes a parameter with the same name', ) sc = functools.partial(scenario, 'when_arguments.feature') test_argument_in_when_step_1 = sc('Argument in when, step 1') test_argument_in_when_step_2 = sc('Argument in when, step 2') @pytest.fixture def values(): return ['1', '2', '1', '0', '999999'] @given(re.compile(r'I have (?P<euro>\d+) Euro')) def i_have(euro, values): assert euro == values.pop(0) @when(re.compile(r'I pay (?P<euro>\d+) Euro')) def i_pay(euro, values, request):
def test_wrong(request, feature, scenario_name): """Test wrong feature scenarios.""" sc = scenario(feature, scenario_name) with pytest.raises(FeatureError): sc(request)
"""Test step alias when decorated multiple times.""" from pytest_bdd import scenario, given, when, then test_steps = scenario('alias.feature', 'Multiple decorated aliases should work') @given('Given I have an empty list') def results(): return [] @given('I have foo (which is 1) in my list') @given('I have bar (alias of foo) in my list') def foo(results): results.append(1) @when('I do crash (which is 2)') @when('I do boom (alias of crash)') def crash(results): results.append(2) @then('my list should be [1, 2, 2]') def check_results(results): """Fixture is evaluated only once, so the list will be [1, 2, 2]""" assert results == [1, 2, 2]
from pytest_bdd import scenario, given, when, then test_google_ngrams_build = scenario( 'google_ngrams.feature', 'Build a vector space from coocurrence matrix', ) test_google_ngrams_tf_idf = scenario( 'google_ngrams.feature', 'Apply tf-id weighting to the co-occurrence matrix', ) test_google_ngrams_line_normalize = scenario( 'google_ngrams.feature', 'Line-normalize the co-occurrence matrix', ) test_google_ngrams_nmf = scenario( 'google_ngrams.feature', 'Reduce the co-occurrence matrix using NMF', ) @when('I build a co-occurrence matrix') def build_cooccurrence_matrix(dispatcher, cooccurrence_dir_path, store_path, context_path, targets_path): assert dispatcher.dispatch( 'google-ngrams cooccurrence ' '--context {context_path} '
"""Test step alias when decorated multiple times.""" from pytest_bdd import scenario, given, when, then test_steps = scenario('alias.feature', 'Multiple given alias is not evaluated multiple times') @given('Given I have an empty list') def results(): return [] @given('I have foo (which is 1) in my list') @given('I have bar (alias of foo) in my list') def foo(results): results.append(1) @when('I do crash (which is 2)') @when('I do boom (alias of crash)') def crash(results): results.append(2) @then('my list should be [1, 2, 2]') def check_results(results): """Fixtures are not evaluated multiple times, so the list will be [1, 2, 2]""" assert results == [1, 2, 2]
from pytest_bdd import scenario, when, then test_entry_is_displayed = scenario( "entry/entry.feature", "Entry is displayed", ) test_no_form_for_visitors = scenario( "entry/entry.feature", "No form for visitors", ) test_create_entry = scenario( "entry/entry.feature", "Create an entry", ) @then("I should see my entry") def should_see_entry(browser, title): assert browser.is_text_present(title) @then("I shouldn't see an entry form") def shoudnt_see_entry_form(browser): assert not browser.find_by_css(".add-entry") @when("I enter entry title") def enter_entry_title(browser, title): browser.fill('title', title)
from pytest_bdd import scenario, given, then test_steps = scenario( 'args.feature', 'Executed with steps matching step definitons with arguments', ) @given('I have a foo fixture with value "foo"') def foo(): return 'foo' @given('there is a list') def results(): return [] @then('foo should have value "foo"') def foo_is_foo(foo): assert foo == 'foo' @then('the list should be [1, 2, 3]') def check_results(results): assert results == [1, 2, 3]
from pytest_bdd import scenario, given, when, then test_google_ngrams_build = scenario( 'google_ngrams.feature', 'Build a vector space from coocurrence matrix', ) test_google_ngrams_tf_idf = scenario( 'google_ngrams.feature', 'Apply tf-id weighting to the co-occurrence matrix', ) test_google_ngrams_line_normalize = scenario( 'google_ngrams.feature', 'Line-normalize the co-occurrence matrix', ) test_google_ngrams_nmf = scenario( 'google_ngrams.feature', 'Reduce the co-occurrence matrix using NMF', ) @when('I build a co-occurrence matrix') def build_cooccurrence_matrix(dispatcher, cooccurrence_dir_path, store_path, context_path, targets_path): assert dispatcher.dispatch('google-ngrams cooccurrence ' '--context {context_path} ' '--targets {targets_path} ' '-i {cooccurrence_dir_path} ' '-o {output_path} '
from pytest_bdd import scenario, given, when, then test_steps = scenario('steps.feature', 'Executed step by step') @given('I have a foo fixture with value "foo"') def foo(): return 'foo' @given('there is a list') def results(): return [] @when('I append 1 to the list') def append_1(results): results.append(1) @when('I append 2 to the list') def append_2(results): results.append(2) @when('I append 3 to the list') def append_3(results): results.append(3)
import pytest from pytest_bdd import given, then, scenario from pytest_bdd.steps import StepError @given('I have foo') def foo(): return 'foo' given('I have alias for foo', fixture='foo') given('I have an alias to the root fixture', fixture='root') test_given_with_fixture = scenario('given.feature', 'Test reusing local fixture') test_root_alias = scenario('given.feature', 'Test reusing root fixture') @then('foo should be "foo"') def foo_is_foo(foo): assert foo == 'foo' @then('root should be "root"') def root_is_root(root): assert root == 'root' def test_decorate_with_fixture(): """Test given can't be used as decorator when the fixture is specified."""
def test_wrong_type_order(request, scenario_name): """Test wrong step type order.""" sc = scenario('wrong_type_order.feature', scenario_name) with pytest.raises(StepTypeError) as excinfo: sc(request) excinfo # TODO: assert the exception args from parameters
from pytest_bdd import scenario, given, when, then test_steps = scenario('regex.feature', 'Executed with steps matching regex step definitons') @given('I have a foo fixture with value "foo"') def foo(): return 'foo' @given('there is a list') def results(): return [] @when('I append (?P<n>\d+) to the list') def append_to_list(results, n): results.append(int(n)) @then('foo should have value "foo"') def foo_is_foo(foo): assert foo == 'foo' @then('the list should be [1, 2, 3]') def check_results(results): assert results == [1, 2, 3]
"""Test step alias when decorated multiple times.""" from pytest_bdd import scenario, given, when, then test_steps = scenario('alias.feature', 'Multiple given alias is not evaluated multiple times') @given('I have an empty list') def results(): return [] @given('I have foo (which is 1) in my list') @given('I have bar (alias of foo) in my list') def foo(results): results.append(1) @when('I do crash (which is 2)') @when('I do boom (alias of crash)') def crash(results): results.append(2) @then('my list should be [1, 2, 2]') def check_results(results): """Fixtures are not evaluated multiple times, so the list will be [1, 2, 2]""" assert results == [1, 2, 2]
def test_feature_path(request, scenario_name): """Test feature base dir.""" with pytest.raises(IOError) as exc: scenario("steps.feature", scenario_name) assert os.path.join("/does/not/exist/", "steps.feature") in str(exc.value)
def test_wrong(request, scenario_name): """Test wrong feature scenarios.""" sc = scenario('wrong.feature', scenario_name) with pytest.raises(FeatureError): sc(request)
from pytest_bdd.steps import when from pytest_bdd import given, then, scenario test_reuse = scenario( 'reuse.feature', 'Given and when using the same fixture should not evaluate it twice', ) @given('I have an empty list') def empty_list(): return [] @given('I have a fixture (appends 1 to a list)') def appends_1(empty_list): empty_list.append(1) return empty_list given('I have a fixture (appends 1 to a list) in reuse syntax', fixture='appends_1') @when('I use this fixture') def use_fixture(appends_1): pass @then('my list should be [1]') def list_should_be_1(appends_1): assert appends_1 == [1]