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
20 changes: 10 additions & 10 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from __future__ import absolute_import, division, print_function
import os
import sys
import textwrap
import types

import six

import _pytest._code
import py
import pytest
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR
Expand Down Expand Up @@ -201,16 +201,16 @@ def test_chdir(self, testdir):
testdir.tmpdir.join("py").mksymlinkto(py._pydir)
p = testdir.tmpdir.join("main.py")
p.write(
_pytest._code.Source(
textwrap.dedent(
"""\
import sys, os
sys.path.insert(0, '')
import py
print(py.__file__)
print(py.__path__)
os.chdir(os.path.dirname(os.getcwd()))
print(py.log)
"""
import sys, os
sys.path.insert(0, '')
import py
print (py.__file__)
print (py.__path__)
os.chdir(os.path.dirname(os.getcwd()))
print (py.log)
"""
)
)
result = testdir.runpython(p)
Expand Down
9 changes: 3 additions & 6 deletions testing/code/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import _pytest._code
import pytest
import mock
from test_excinfo import TWMock
from six import text_type

Expand Down Expand Up @@ -67,12 +68,8 @@ def func():

f = func()
f = _pytest._code.Frame(f)
prop = f.code.__class__.fullsource
try:
f.code.__class__.fullsource = None
assert f.statement == _pytest._code.Source("")
finally:
f.code.__class__.fullsource = prop
with mock.patch.object(f.code.__class__, "fullsource", None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use monkeypatch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops didn't see this until already merged -- monkeypatch has a much wider scope than the code I was replacing and so it is not equivalent.

(And since this is monkeypatching a pytest internal I don't really want that to leak out of this test)

Curious also why to prefer monkeypatch over mock.patch -- the latter is much more battle tested and has a well defined patch scope due to the with block

assert f.statement == ""


def test_code_from_func():
Expand Down
8 changes: 4 additions & 4 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def xyz():
except somenoname:
pass
xyz()
"""
"""
)
try:
exec(source.compile())
Expand Down Expand Up @@ -426,7 +426,7 @@ class TestFormattedExcinfo(object):
@pytest.fixture
def importasmod(self, request):
def importasmod(source):
source = _pytest._code.Source(source)
source = textwrap.dedent(source)
tmpdir = request.getfixturevalue("tmpdir")
modpath = tmpdir.join("mod.py")
tmpdir.ensure("__init__.py")
Expand All @@ -450,10 +450,10 @@ def excinfo_from_exec(self, source):
def test_repr_source(self):
pr = FormattedExcinfo()
source = _pytest._code.Source(
"""
"""\
def f(x):
pass
"""
"""
).strip()
pr.flow_marker = "|"
lines = pr.get_source(source, 0)
Expand Down
137 changes: 69 additions & 68 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import os
import sys
from textwrap import dedent
import textwrap

import _pytest._code
import pytest
Expand Down Expand Up @@ -47,13 +47,14 @@ def test_import_prepend_append(self, testdir, monkeypatch):
p = root2.join("test_x456.py")
monkeypatch.syspath_prepend(str(root1))
p.write(
dedent(
textwrap.dedent(
"""\
import x456
def test():
assert x456.__file__.startswith(%r)
"""
% str(root2)
import x456
def test():
assert x456.__file__.startswith({!r})
""".format(
str(root2)
)
)
)
with root2.as_cwd():
Expand Down Expand Up @@ -929,23 +930,23 @@ def pytest_pycollect_makemodule(path, parent):
def test_customized_pymakemodule_issue205_subdir(self, testdir):
b = testdir.mkdir("a").mkdir("b")
b.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makemodule():
outcome = yield
mod = outcome.get_result()
mod.obj.hello = "world"
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makemodule():
outcome = yield
mod = outcome.get_result()
mod.obj.hello = "world"
"""
)
)
b.join("test_module.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
def test_hello():
assert hello == "world"
"""
def test_hello():
assert hello == "world"
"""
)
)
reprec = testdir.inline_run()
Expand All @@ -954,31 +955,31 @@ def test_hello():
def test_customized_pymakeitem(self, testdir):
b = testdir.mkdir("a").mkdir("b")
b.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makeitem():
outcome = yield
if outcome.excinfo is None:
result = outcome.get_result()
if result:
for func in result:
func._some123 = "world"
"""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_pycollect_makeitem():
outcome = yield
if outcome.excinfo is None:
result = outcome.get_result()
if result:
for func in result:
func._some123 = "world"
"""
)
)
b.join("test_module.py").write(
_pytest._code.Source(
"""
import pytest
textwrap.dedent(
"""\
import pytest

@pytest.fixture()
def obj(request):
return request.node._some123
def test_hello(obj):
assert obj == "world"
"""
@pytest.fixture()
def obj(request):
return request.node._some123
def test_hello(obj):
assert obj == "world"
"""
)
)
reprec = testdir.inline_run()
Expand Down Expand Up @@ -1033,7 +1034,7 @@ def pytest_collect_file(path, parent):
)
testdir.makefile(
".narf",
"""
"""\
def test_something():
assert 1 + 1 == 2""",
)
Expand All @@ -1046,29 +1047,29 @@ def test_setup_only_available_in_subdir(testdir):
sub1 = testdir.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2")
sub1.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub1"
"""
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub1"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub1"
"""
)
)
sub2.join("conftest.py").write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub2"
"""
import pytest
def pytest_runtest_setup(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_call(item):
assert item.fspath.purebasename == "test_in_sub2"
def pytest_runtest_teardown(item):
assert item.fspath.purebasename == "test_in_sub2"
"""
)
)
sub1.join("test_in_sub1.py").write("def test_1(): pass")
Expand Down Expand Up @@ -1547,12 +1548,12 @@ def test_skip_duplicates_by_default(testdir):
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def test_real():
pass
"""
import pytest
def test_real():
pass
"""
)
)
result = testdir.runpytest(a.strpath, a.strpath)
Expand All @@ -1567,12 +1568,12 @@ def test_keep_duplicates(testdir):
a = testdir.mkdir("a")
fh = a.join("test_a.py")
fh.write(
_pytest._code.Source(
textwrap.dedent(
"""\
import pytest
def test_real():
pass
"""
import pytest
def test_real():
pass
"""
)
)
result = testdir.runpytest("--keep-duplicates", a.strpath, a.strpath)
Expand Down
Loading