Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,33 @@ def twenty_cls_tests():
for i in range(20):
code.append("\tdef test_b{0}(self): self.assertTrue\n".format(str(i).zfill(2)))
return "".join(code)


def _deindent_source(source):
"""
A minimal replacement for py.code.Source to deindent inlined test code.
Looks for the first non-empty line to determine deindent offset, doesn't
attempt to understand the code, line continuations, etc.
"""
lines = source.splitlines()
for line in lines:
stripped = line.lstrip()
if stripped:
offset = len(line) - len(stripped)
break
else:
offset = 0

output_lines = []
for line in lines:
output_lines.append(line[offset:])

return "\n".join(output_lines)


@pytest.fixture
def deindent_source():
"""
Returns a helper function to deindent inlined source code.
"""
return _deindent_source
15 changes: 7 additions & 8 deletions tests/test_actual_test_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import collections
import re

import py
import pytest


@pytest.fixture
def tmp_tree_of_tests(testdir):
def tmp_tree_of_tests(testdir, deindent_source):
"""
Creates a directory structure:
tmpdir/
Expand All @@ -25,7 +24,7 @@ def tmp_tree_of_tests(testdir):
sup = testdir.mkpydir("shallow_tests")

sup.join("test_a.py").write(
py.code.Source("""
deindent_source("""
def test_a1():
assert False
def test_a2():
Expand All @@ -36,7 +35,7 @@ def test_a3():
)

sup.join("test_ax.py").write(
py.code.Source("""
deindent_source("""
def test_ax1():
assert True
def test_ax2():
Expand All @@ -49,7 +48,7 @@ def test_ax3():
sub = testdir.mkpydir("shallow_tests/deep_tests")

sub.join("test_b.py").write(
py.code.Source("""
deindent_source("""
def test_b1():
assert True
def test_b2():
Expand All @@ -60,14 +59,14 @@ def test_b3():
)

sub.join("test_c.py").write(
py.code.Source("""
deindent_source("""
def test_c1():
assert True
""")
)

sub.join("test_d.py").write(
py.code.Source("""
deindent_source("""
def test_d1():
assert True
def test_d2():
Expand All @@ -76,7 +75,7 @@ def test_d2():
)

sub.join("test_e.py").write(
py.code.Source("""
deindent_source("""
from unittest import TestCase
class EeTest(TestCase):
def test_ee1(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/test_doctests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
import py
import pytest


@pytest.fixture
def tmp_tree_of_tests(testdir):
def tmp_tree_of_tests(testdir, deindent_source):
"""
Creates a directory structure:
tmpdir/
Expand All @@ -16,7 +15,7 @@ def tmp_tree_of_tests(testdir):
utils_package.join("__init__.py").write("")

utils_package.join("foo.py").write(
py.code.Source('''
deindent_source('''
def add(a, b):
"""
>>> add(1, 1)
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ envlist = py{39,310,311,312,313}

[testenv]
deps =
py
pytest-xdist
commands =
pytest tests --color=yes