def test_migrate(monkeypatch, capsys, testdir): """Test if the code is migrated by a given file mask.""" tests = testdir.mkpydir('tests') tests.join("test_foo.py").write(py.code.Source(''' """Foo bar tests.""" from bdd import scenario test_foo = scenario('foo_bar.feature', 'Foo bar') ''')) monkeypatch.setattr(sys, 'argv', ['', 'migrate', tests.strpath]) main() out, err = capsys.readouterr() out = '\n'.join(sorted(out.splitlines())) expected = textwrap.dedent(''' migrated: {0}/test_foo.py skipped: {0}/__init__.py'''.format(tests.strpath)[1:]) assert out == expected assert tests.join("test_foo.py").read() == textwrap.dedent(''' """Foo bar tests.""" from bdd import scenario @scenario('foo_bar.feature', 'Foo bar') def test_foo(): pass ''')
def test_main(monkeypatch, capsys): """Test if main commmand shows help when called without the subcommand.""" monkeypatch.setattr(sys, 'argv', ['pytest-bdd']) monkeypatch.setattr(sys, 'exit', lambda x: x) main() out, err = capsys.readouterr() assert 'usage: pytest-bdd [-h]' in err assert 'pytest-bdd: error:' in err
def test_generate(monkeypatch, capsys): """Test if the code is generated by a given feature.""" monkeypatch.setattr(sys, 'argv', ['', 'generate', os.path.join(PATH, 'generate.feature')]) main() out, err = capsys.readouterr() assert out == textwrap.dedent(''' # coding=utf-8 """Code generation feature tests.""" from 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 have_a_fixture_appends_1_to_a_list_in_reuse_syntax(): """1 have a fixture (appends 1 to a list) in reuse syntax.""" @given('I have an empty list') def i_have_an_empty_list(): """I have an empty list.""" @when('I use this fixture') def i_use_this_fixture(): """I use this fixture.""" @then('my list should be [1]') def my_list_should_be_1(): """my list should be [1].""" '''[1:].replace(u"'", u"'"))