Ejemplo n.º 1
0
def test_local_fixture_overrides(testdir):
    a_dir = testdir.mkpydir("a_dir")
    a_dir.join("test_a.py").write(
        py.code.Source(
            """
        import pytest

        @pytest.fixture
        def thing():
            return 12

        def describe_something():
            def describe_a_nested_scope():
                @pytest.fixture
                def thing():
                    return 42

                def thing_is_42(thing):
                    assert thing == 42

            def thing_is_12(thing):
                assert thing == 12
    """
        )
    )

    result = testdir.runpytest()
    assert_outcomes(result, passed=2)
Ejemplo n.º 2
0
def test_parametrize_with_shared(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            def it_quacks(sound):
                assert sound == int(sound)


        @pytest.mark.parametrize('foo', (1, 2, 3))
        @behaves_like(a_duck)
        def describe_something_that_quacks():
            @fixture
            def sound(foo):
                return foo

        @pytest.mark.parametrize('foo', (1, 2, 3))
        @behaves_like(a_duck)
        def describe_something_that_barks():
            @fixture
            def sound(foo):
                return foo
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=6)
Ejemplo n.º 3
0
def test_special_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        import pytest

        def describe_marks():
            @pytest.mark.xfail
            def xfails():
                assert False

            @pytest.mark.xfail
            def xpasses():
                pass

            @pytest.mark.skipif("0 < 1")
            def skipped():
                pass

            @pytest.mark.parametrize('foo', (1, 2, 3))
            def isint(foo):
                assert foo == int(foo)
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=3, xfailed=1, xpassed=1, skipped=1)
Ejemplo n.º 4
0
def test_shared_behavior(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(
        py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            def it_quacks(sound):
                assert sound == "quack"

        @behaves_like(a_duck)
        def describe_something_that_quacks():
            @fixture
            def sound():
                return "quack"

        @behaves_like(a_duck)
        def describe_something_that_barks():
            @fixture
            def sound():
                return "bark"
    """))

    result = testdir.runpytest()
    assert_outcomes(result, failed=1, passed=1)
Ejemplo n.º 5
0
def test_nested_name_mangling(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def thing():
            def it_does_something():
                pass
            def describe_thing():
                def it_does_something():
                    pass
                def describe_thing():
                    def it_does_something():
                        pass

        @behaves_like(thing)
        def describe_thing():
            def it_does_something():
                pass
            def describe_thing():
                def it_does_something():
                    pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=5)
Ejemplo n.º 6
0
def test_mark_stacking(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        import pytest
        @pytest.fixture()
        def get_marks(request):
            return [(mark.args[0], node.name) for node, mark
                    in request.node.iter_markers_with_node(name='my_mark')]

        @pytest.mark.my_mark('foo')
        def describe_marks():
            def it_is_inherited_from_describe_block(get_marks):
                assert get_marks == [('foo', 'describe_marks')]

            @pytest.mark.my_mark('bar')
            @pytest.mark.my_mark('baz')
            def all_marks_are_chained(get_marks):
                assert get_marks == [
                    ('baz', 'all_marks_are_chained'),
                    ('bar', 'all_marks_are_chained'),
                    ('foo', 'describe_marks')]
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=2)
Ejemplo n.º 7
0
def test_multiple_variables_parametrize(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        import pytest

        def describe_marks():
            @pytest.mark.parametrize('foo,bar', [(1, 2), (3, 4)])
            def isint_str_names(foo, bar):
                assert foo == int(foo)
                assert bar == int(bar)

            @pytest.mark.parametrize(['foo', 'bar'], [(1, 2), (3, 4)])
            def isint_list_names(foo, bar):
                assert foo == int(foo)
                assert bar == int(bar)

            @pytest.mark.parametrize(('foo', 'bar'), [(1, 2), (3, 4)])
            def isint_tuple_names(foo, bar):
                assert foo == int(foo)
                assert bar == int(bar)
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=6)
Ejemplo n.º 8
0
def test_parametrize_with_shared_but_different_values(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        import pytest
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            def it_quacks(sound):
                assert sound[1] == int(sound[1])
                assert sound[0] == 'bark' or sound[1] <= 3
                assert sound[0] == 'quack' or sound[1] >= 4


        @pytest.mark.parametrize('foo', (1, 2, 3))
        @behaves_like(a_duck)
        def describe_something_that_quacks():
            @fixture
            def sound(foo):
                return ('quack', foo)

        @pytest.mark.parametrize('foo', (4, 5, 6))
        @behaves_like(a_duck)
        def describe_something_that_barks():
            @fixture
            def sound(foo):
                return ('bark', foo)
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=6)
Ejemplo n.º 9
0
def test_special_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        def describe_marks():
            @pytest.mark.xfail
            def xfails():
                assert False

            @pytest.mark.xfail
            def xpasses():
                pass

            @pytest.mark.skipif("0 < 1")
            def skipped():
                pass

            @pytest.mark.parametrize('foo', (1, 2, 3))
            def isint(foo):
                assert foo == int(foo)
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=3, xfailed=1, xpassed=1, skipped=1)
Ejemplo n.º 10
0
def test_shared_behavior(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            def it_quacks(sound):
                assert sound == "quack"

        @behaves_like(a_duck)
        def describe_something_that_quacks():
            @fixture
            def sound():
                return "quack"

        @behaves_like(a_duck)
        def describe_something_that_barks():
            @fixture
            def sound():
                return "bark"
    """))

    result = testdir.runpytest()
    assert_outcomes(result, failed=1, passed=1)
Ejemplo n.º 11
0
def test_nested_name_mangling(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(
        py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def thing():
            def it_does_something():
                pass
            def describe_thing():
                def it_does_something():
                    pass
                def describe_thing():
                    def it_does_something():
                        pass

        @behaves_like(thing)
        def describe_thing():
            def it_does_something():
                pass
            def describe_thing():
                def it_does_something():
                    pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=5)
def test_can_pass(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        def describe_something():
            def passes():
                assert True
            def describe_nested():
                def passes_too():
                    assert True
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=2)
Ejemplo n.º 13
0
def test_can_pass(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        def describe_something():
            def passes():
                assert True
            def describe_nested():
                def passes_too():
                    assert True
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=2)
def test_can_fail_and_pass(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        def describe_something():
            def describe_nested_ok():
                def passes():
                    assert True
            def describe_nested_bad():
                def fails():
                    assert False
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=1, failed=1)
Ejemplo n.º 15
0
def test_module_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        pytestmark = [ pytest.mark.foo ]
        def describe_a():
            pytestmark = [ pytest.mark.bar ]
            def describe_b():
                def a_test():
                    pass
    """))

    result = testdir.runpytest('-m', 'foo')
    assert_outcomes(result, passed=1)
Ejemplo n.º 16
0
def test_module_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        pytestmark = [ pytest.mark.foo ]
        def describe_a():
            pytestmark = [ pytest.mark.bar ]
            def describe_b():
                def a_test():
                    pass
    """))

    result = testdir.runpytest('-k', 'foo')
    assert_outcomes(result, passed=1)
Ejemplo n.º 17
0
def test_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        def describe_a():
            @pytest.mark.foo
            def foo_test():
                pass
            @pytest.mark.bar
            def bar_test():
                pass
    """))

    result = testdir.runpytest('-m', 'foo')
    assert_outcomes(result, passed=1, deselected=1)
Ejemplo n.º 18
0
def test_can_fail_and_pass(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(
        py.code.Source("""
        def describe_something():
            def describe_nested_ok():
                def passes():
                    assert True
            def describe_nested_bad():
                def fails():
                    assert False
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=1, failed=1)
Ejemplo n.º 19
0
def test_keywords(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        def describe_a():
            @pytest.mark.foo
            def foo_test():
                pass
            @pytest.mark.bar
            def bar_test():
                pass
    """))

    result = testdir.runpytest('-k', 'foo')
    assert_outcomes(result, passed=1, deselected=1)
Ejemplo n.º 20
0
def test_can_access_local_fixture(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        def describe_something():
            @pytest.fixture
            def thing():
                return 42

            def thing_is_42(thing):
                assert thing == 42
    """))

    result = testdir.runpytest('')
    assert_outcomes(result, passed=1)
Ejemplo n.º 21
0
def test_mark_at_describe_function(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        @pytest.mark.foo
        def describe_foo():
            def describe_a():
                def a_test():
                    pass
            @pytest.mark.bar
            def b_test():
                pass
    """))

    result = testdir.runpytest('-k', 'foo')
    assert_outcomes(result, passed=2)
Ejemplo n.º 22
0
def test_mark_at_describe_function(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest
        @pytest.mark.foo
        def describe_foo():
            def describe_a():
                def a_test():
                    pass
            @pytest.mark.bar
            def b_test():
                pass
    """))

    result = testdir.runpytest('-m', 'foo')
    assert_outcomes(result, passed=2)
Ejemplo n.º 23
0
def test_can_access_local_fixture(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        def describe_something():
            @pytest.fixture
            def thing():
                return 42

            def thing_is_42(thing):
                assert thing == 42
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=1)
Ejemplo n.º 24
0
def test_cartesian_parametrize_on_describe(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        @pytest.mark.parametrize('foo', (1, 2, 3))
        @pytest.mark.parametrize('bar', (1, 2, 3))
        def describe_marks():

            def isint(foo, bar):
                assert foo == int(foo)
                assert bar == int(bar)
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=9)
Ejemplo n.º 25
0
def test_describe_evaluated_once(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(py.code.Source("""
        count = 0
        def describe_is_evaluated_only_once():
            global count
            count += 1
            def one():
                assert count == 1
            def two():
                assert count == 1
            def describe_nested():
                def three():
                    assert count == 1
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=3)
Ejemplo n.º 26
0
def test_fixture(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            @fixture
            def sound():
                return "quack"

            def it_quacks(sound):
                assert sound == "quack"

        @behaves_like(a_duck)
        def describe_a_normal_duck():
            pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=1)
Ejemplo n.º 27
0
def test_can_access_fixture_from_nested_scope(testdir):
    a_dir = testdir.mkpydir("a_dir")
    a_dir.join("test_a.py").write(
        py.code.Source(
            """
        import pytest

        def describe_something():
            @pytest.fixture
            def thing():
                return 42

            def describe_a_nested_scope():
                def thing_is_42(thing):
                    assert thing == 42
    """
        )
    )

    result = testdir.runpytest()
    assert_outcomes(result, passed=1)
Ejemplo n.º 28
0
def test_dependence_marks(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        def test_that_succedes():
            assert True

        @pytest.mark.dependends_on("test_that_succedes")
        def test_that_has_depend_ok():
            assert True

        def test_that_fails():
            assert False

        @pytest.mark.dependends_on("test_that_fails")
        def test_that_has_depend_failed():
            assert False
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=2, failed=1, skipped=1)
Ejemplo n.º 29
0
def test_fixture(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(
        py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        def a_duck():
            @fixture
            def sound():
                return "quack"

            def it_quacks(sound):
                assert sound == "quack"

        @behaves_like(a_duck)
        def describe_a_normal_duck():
            pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=1)
Ejemplo n.º 30
0
def test_evaluated_once(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        count = 0
        def thing():
            global count
            count += 1
            def is_evaluated_once():
                assert count == 1

        @behaves_like(thing)
        def describe_something():
            pass
        @behaves_like(thing)
        def describe_something_else():
            pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=2)
Ejemplo n.º 31
0
def test_evaluated_once(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_something.py').write(
        py.code.Source("""
        from pytest import fixture
        from pytest_describe import behaves_like

        count = 0
        def thing():
            global count
            count += 1
            def is_evaluated_once():
                assert count == 1

        @behaves_like(thing)
        def describe_something():
            pass
        @behaves_like(thing)
        def describe_something_else():
            pass
    """))

    result = testdir.runpytest('-v')
    assert_outcomes(result, passed=2)
Ejemplo n.º 32
0
def test_local_fixture_overrides(testdir):
    a_dir = testdir.mkpydir('a_dir')
    a_dir.join('test_a.py').write(py.code.Source("""
        import pytest

        @pytest.fixture
        def thing():
            return 12

        def describe_something():
            def describe_a_nested_scope():
                @pytest.fixture
                def thing():
                    return 42

                def thing_is_42(thing):
                    assert thing == 42

            def thing_is_12(thing):
                assert thing == 12
    """))

    result = testdir.runpytest()
    assert_outcomes(result, passed=2)