Skip to content

Commit

Permalink
Simplify all tests and make them a lot cleaner
Browse files Browse the repository at this point in the history
This also completely removes the necessity to use the
py.code module and the internal test.util module.
  • Loading branch information
Cito committed Apr 9, 2023
1 parent 68a478c commit 826a9b1
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 220 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ on:

jobs:
build:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest

strategy:
matrix:
python: ['3.7', '3.8', '3.9', '3.10', 'py3.11', 'pypy3.9']
python: ['3.7', '3.8', '3.9', '3.10', '3.11', 'pypy3.9']

steps:
- uses: actions/checkout@v3
Expand All @@ -36,31 +36,31 @@ jobs:
- name: Test with Python 3.7
if: matrix.python == '3.7'
run: tox -e 'py37-pytest{4,5,60,61,62,70,71,72,73}'
run: tox run -x "tox.envlist=py37-pytest{4,5,60,61,62,70,71,72,73}"

- name: Test with Python 3.8
if: matrix.python == '3.8'
run: tox -e 'py38-pytest{4,5,60,61,62,70,71,72,73}'
run: tox run -x "tox.envlist=py38-pytest{4,5,60,61,62,70,71,72,73}"

- name: Test with Python 3.9
if: matrix.python == '3.9'
run: tox -e 'py39-pytest{4,5,60,61,62,70,71,72,73}'
run: tox run -x "tox.envlist=py39-pytest{4,5,60,61,62,70,71,72,73}"

- name: Test with Python 3.10
if: matrix.python == '3.10'
run: tox -e 'py310-pytest{62,70,71,72,73}'
run: tox run -x "tox.envlist=py310-pytest{62,70,71,72,73}"

- name: Test with Python 3.11
if: matrix.python == '3.11'
run: tox -e 'py311-pytest{73}'
run: tox run -x "tox.envlist=spy311-pytest{73}"

- name: Test with PyPy 3.9
if: matrix.python == 'pypy3.9'
run: tox -e 'pypy39-pytest{4,5,60,61,62,70,71,72,73}'
run: tox run -x "tox.envlist=pypy39-pytest{4,5,60,61,62,70,71,72,73}"

- name: Linting with Flake8
if: matrix.python == '3.10'
run: tox -e flake8
run: tox run -e flake8

deploy:
if: |
Expand Down
3 changes: 3 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Global fixtures for testing the plugin"""

pytest_plugins = ["pytester"]
49 changes: 23 additions & 26 deletions test/test_collect.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import re
"""Test collection of test functions"""

from util import assert_outcomes, Source

pytest_plugins = 'pytester'


def test_collect(testdir):
a_dir = testdir.mkpydir('a_dir')
a_dir.join('test_a.py').write(Source("""
def test_collect_only(testdir):
testdir.makepyfile(
"""
def describe_something():
def is_foo():
pass
Expand All @@ -23,27 +19,28 @@ def foo_not_collected():
pass
def test_something():
pass
"""))
""")

result = testdir.runpytest('--collectonly')

expected_regex = map(re.compile, [
r"collected 4 item(s)?",
r"\s*<DescribeBlock '?describe_something'?>",
r"\s*<Function '?is_foo'?>",
r"\s*<Function '?can_bar'?>",
r"\s*<DescribeBlock '?describe_something_else'?>",
r"\s*<DescribeBlock '?describe_nested'?>",
r"\s*<Function '?a_test'?>",
r"\s*<Function '?test_something'?>",
])
for line in expected_regex:
assert any([line.match(r) is not None for r in result.outlines])
result.assert_outcomes()

output = '\n'.join(filter(None, result.outlines))
assert """
collected 4 items
<Module test_collect_only.py>
<DescribeBlock 'describe_something'>
<Function is_foo>
<Function can_bar>
<DescribeBlock 'describe_something_else'>
<DescribeBlock 'describe_nested'>
<Function a_test>
<Function test_something>
""" in output


def test_describe_evaluated_once(testdir):
a_dir = testdir.mkpydir('a_dir')
a_dir.join('test_something.py').write(Source("""
testdir.makepyfile(
"""
count = 0
def describe_is_evaluated_only_once():
global count
Expand All @@ -55,7 +52,7 @@ def two():
def describe_nested():
def three():
assert count == 1
"""))
""")

result = testdir.runpytest('-v')
assert_outcomes(result, passed=3)
result.assert_outcomes(passed=3)
33 changes: 0 additions & 33 deletions test/test_custom_prefix.py

This file was deleted.

28 changes: 13 additions & 15 deletions test/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from util import assert_outcomes, Source

pytest_plugins = 'pytester'
"""Test with fixtures"""


def test_can_access_local_fixture(testdir):
a_dir = testdir.mkpydir('a_dir')
a_dir.join('test_a.py').write(Source("""
testdir.makepyfile(
"""
import pytest
def describe_something():
Expand All @@ -15,15 +13,15 @@ def thing():
def thing_is_42(thing):
assert thing == 42
"""))
""")

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


def test_can_access_fixture_from_nested_scope(testdir):
a_dir = testdir.mkpydir('a_dir')
a_dir.join('test_a.py').write(Source("""
testdir.makepyfile(
"""
import pytest
def describe_something():
Expand All @@ -34,15 +32,15 @@ def thing():
def describe_a_nested_scope():
def thing_is_42(thing):
assert thing == 42
"""))
""")

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


def test_local_fixture_overrides(testdir):
a_dir = testdir.mkpydir('a_dir')
a_dir.join('test_a.py').write(Source("""
testdir.makepyfile(
"""
import pytest
@pytest.fixture
Expand All @@ -60,7 +58,7 @@ def thing_is_42(thing):
def thing_is_12(thing):
assert thing == 12
"""))
""")

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

0 comments on commit 826a9b1

Please sign in to comment.