[svn r57762] * settle on using suptest for supporting test runs

* fix a bug in getmodpath
* remove redundant functions/files

--HG--
branch : trunk
This commit is contained in:
hpk 2008-09-02 16:31:42 +02:00
parent 7518dcabc2
commit 9eb1d55380
11 changed files with 65 additions and 278 deletions

View File

@ -43,9 +43,13 @@ class PyobjMixin(object):
for node in chain: for node in chain:
if isinstance(node, Instance): if isinstance(node, Instance):
continue continue
if stopatmodule and isinstance(node, Module): name = node.name
break if isinstance(node, Module):
parts.append(node.name) if stopatmodule:
break
assert name.endswith(".py")
name = name[:-3]
parts.append(name)
parts.reverse() parts.reverse()
s = ".".join(parts) s = ".".join(parts)
return s.replace(".[", "[") return s.replace(".[", "[")

View File

@ -64,22 +64,6 @@ class BaseReporter(object):
l.append((len(events),) + key) l.append((len(events),) + key)
return l return l
def getmodpath(pycolitem):
""" return dotted module path for the given colitem. """
colitems = pycolitem.listchain()
while colitems:
colitem = colitems.pop(0)
if isinstance(colitem, colitem.Module):
parts = [colitem.obj.__name__]
for colitem in colitems:
if colitem.name[0] in '([':
parts[-1] += colitem.name
else:
parts.append(colitem.name)
return ".".join(parts)
return colitem.name
def repr_pythonversion(v=None): def repr_pythonversion(v=None):
if v is None: if v is None:
v = sys.version_info v = sys.version_info

View File

@ -2,7 +2,7 @@ import py
import sys import sys
from py.__.test import event from py.__.test import event
from py.__.test.report.base import BaseReporter from py.__.test.report.base import BaseReporter
from py.__.test.report.base import getrelpath, repr_pythonversion, getmodpath from py.__.test.report.base import getrelpath, repr_pythonversion
class TerminalReporter(BaseReporter): class TerminalReporter(BaseReporter):
def __init__(self, config, file=None, bus=None): def __init__(self, config, file=None, bus=None):

View File

@ -3,8 +3,7 @@ from py.__.test.report.base import BaseReporter
from py.__.test.event import EventBus from py.__.test.event import EventBus
from py.__.test import event from py.__.test import event
from py.__.test.runner import OutcomeRepr from py.__.test.runner import OutcomeRepr
from py.__.test.report.base import getrelpath, getmodpath, repr_pythonversion from py.__.test.report.base import getrelpath, repr_pythonversion
from py.__.test.testing import setupdata
import sys import sys
class TestBaseReporter: class TestBaseReporter:
@ -73,29 +72,6 @@ class TestBaseReporter:
assert lineno == longrepr.lineno assert lineno == longrepr.lineno
assert reason == longrepr.message assert reason == longrepr.message
def test_getmodpath_cases():
tmpdir = py.test.ensuretemp("test_getmodpath_cases")
pkgdir = tmpdir.join("test_getmodpath")
pkgdir.ensure("__init__.py")
nopkgdir = tmpdir.ensure("nopkg", dir=1)
def checkpkg(names, expected):
fcol = setupdata.getexamplecollector(names, tmpdir=pkgdir)
assert getmodpath(fcol) == pkgdir.basename + "." + expected
def checknopkg(names, expected):
fcol = setupdata.getexamplecollector(names, tmpdir=nopkgdir)
assert getmodpath(fcol) == expected
for names in (
'test_mod.py test_f1 test_mod.test_f1',
'test_mod.py TestA () test_m1 test_mod.TestA().test_m1',
'test_mod.py test_g1 test_mod.test_g1',
'test_mod.py test_g1 [0] test_mod.test_g1[0]',
):
names = names.split()
expected = names.pop()
yield checkpkg, names, expected
yield checknopkg, names, expected
def test_repr_python_version(): def test_repr_python_version():
py.magic.patch(sys, 'version_info', (2, 5, 1, 'final', 0)) py.magic.patch(sys, 'version_info', (2, 5, 1, 'final', 0))
try: try:

View File

@ -1,173 +0,0 @@
import py
#def setup_module(mod):
# mod.datadir = setupdatadir()
# mod.tmpdir = py.test.ensuretemp(mod.__name__)
#def setupdatadir():
# datadir = py.test.ensuretemp("datadir")
# for name in namecontent:
# getexamplefile(name)
# return datadir
def getexamplefile(basename, tmpdir=None):
if tmpdir is None:
tmpdir = py.test.ensuretemp("example")
tmpdir.ensure("__init__.py")
path = tmpdir.join(basename)
if not path.check():
path.write(py.code.Source(namecontent[basename]))
print "creating testfile", path
return path
def getexamplecollector(names, tmpdir=None):
fn = getexamplefile(names[0], tmpdir=tmpdir)
config = py.test.config._reparse([fn.dirpath()])
col = config.getfsnode(fn)
return col._getitembynames(names[1:])
namecontent = {
'syntax_error.py': "this is really not python\n",
'disabled_module.py': '''
disabled = True
def setup_module(mod):
raise ValueError
class TestClassOne:
def test_func(self):
raise ValueError
class TestClassTwo:
def setup_class(cls):
raise ValueError
def test_func(self):
raise ValueError
''',
'brokenrepr.py': '''
import py
class BrokenRepr1:
"""A broken class with lots of broken methods. Let's try to make the test framework
immune to these."""
foo=0
def __repr__(self):
raise Exception("Ha Ha fooled you, I'm a broken repr().")
class BrokenRepr2:
"""A broken class with lots of broken methods. Let's try to make the test framework
immune to these."""
foo=0
def __repr__(self):
raise "Ha Ha fooled you, I'm a broken repr()."
class TestBrokenClass:
def test_explicit_bad_repr(self):
t = BrokenRepr1()
py.test.raises(Exception, 'repr(t)')
def test_implicit_bad_repr1(self):
t = BrokenRepr1()
assert t.foo == 1
def test_implicit_bad_repr2(self):
t = BrokenRepr2()
assert t.foo == 1
''',
'failingimport.py': "import gruetzelmuetzel\n",
'test_mod.py': """
class TestA:
def test_m1(self):
pass
def test_f1():
pass
def test_g1():
yield lambda x: None, 42
""",
'file_test.py': """
def test_one():
assert 42 == 43
class TestClass(object):
def test_method_one(self):
assert 42 == 43
""",
'test_threepass.py': """
def test_one():
assert 1
def test_two():
assert 1
def test_three():
assert 1
""",
'testspecial_importerror.py': """
import asdasd
""",
'disabled.py': """
class TestDisabled:
disabled = True
def test_method(self):
pass
""",
'test_funcexamples.py': """
import py
import time
def test_funcpassed():
pass
def test_funcfailed():
raise AssertionError("hello world")
def test_funcskipped():
py.test.skip("skipped")
def test_funcprint():
print "samfing"
def test_funcprinterr():
print >>py.std.sys.stderr, "samfing"
def test_funcprintfail():
print "samfing elz"
asddsa
def test_funcexplicitfail():
py.test.fail("3")
def test_funcraisesfails():
py.test.raises(ValueError, lambda: 123)
def test_funcoptioncustom():
assert py.test.config.getvalue("custom")
def test_funchang():
import time
time.sleep(1000)
def test_funckill15():
import os
os.kill(os.getpid(), 15)
""",
'docexample.txt': """
Aha!!!!!!
=========
""",
}

View File

@ -6,8 +6,6 @@
for analyzing events an EventSorter instance is returned for both of: for analyzing events an EventSorter instance is returned for both of:
* events_from_cmdline(args): inprocess-run of cmdline invocation * events_from_cmdline(args): inprocess-run of cmdline invocation
* events_from_session(session): inprocess-run of given session * events_from_session(session): inprocess-run of given session
* events_run_example(examplename, *args):in-process-run of
given example test file
eventappender(config): for getting all events in a list: eventappender(config): for getting all events in a list:
""" """
@ -49,11 +47,6 @@ def events_from_session(session):
session.main(getcolitems(session.config)) session.main(getcolitems(session.config))
return sorter return sorter
def events_run_example(examplename, *args):
from setupdata import getexamplefile
p = getexamplefile(examplename)
return events_from_cmdline([p] + list(args))
class EventSorter(object): class EventSorter(object):
def __init__(self, config, session=None): def __init__(self, config, session=None):
self.config = config self.config = config
@ -217,6 +210,13 @@ class InlineCollection(FileCreation):
runner = self.getrunner() runner = self.getrunner()
return runner(item, **runnerargs) return runner(item, **runnerargs)
class InlineSession(InlineCollection):
def parse_and_run(self, *args):
config = self.parseconfig(*args)
session = config.initsession()
sorter = EventSorter(config, session)
session.main()
return sorter
def popvalue(stringio): def popvalue(stringio):
value = stringio.getvalue().rstrip() value = stringio.getvalue().rstrip()

View File

@ -1,7 +1,7 @@
from __future__ import generators from __future__ import generators
import py import py
from py.__.test import event, outcome from py.__.test import event, outcome
import setupdata, suptest from py.__.test.testing import suptest
from py.__.test.conftesthandle import Conftest from py.__.test.conftesthandle import Conftest
from py.__.test.collect import SetupState from py.__.test.collect import SetupState
from test_config import getcolitems from test_config import getcolitems
@ -376,7 +376,6 @@ def test_function_equality():
assert f1 == f1_b assert f1 == f1_b
assert not f1 != f1_b assert not f1 != f1_b
class Testgenitems: class Testgenitems:
def setup_class(cls): def setup_class(cls):
cls.classtemp = py.test.ensuretemp(cls.__name__) cls.classtemp = py.test.ensuretemp(cls.__name__)
@ -462,6 +461,15 @@ class Testgenitems:
assert items[1].name == 'test_method_one' assert items[1].name == 'test_method_one'
assert items[2].name == 'test_method_one' assert items[2].name == 'test_method_one'
# let's also test getmodpath here
assert items[0].getmodpath() == "test_one"
assert items[1].getmodpath() == "TestX.test_method_one"
assert items[2].getmodpath() == "TestY.test_method_one"
s = items[0].getmodpath(stopatmodule=False)
assert s == "test_example_items1.test_example.test_one"
print s
def test_collect_doctest_files_with_test_prefix(self): def test_collect_doctest_files_with_test_prefix(self):
self.tmp.ensure("whatever.txt") self.tmp.ensure("whatever.txt")
checkfile = self.tmp.ensure("test_something.txt") checkfile = self.tmp.ensure("test_something.txt")

View File

@ -2,7 +2,7 @@ from __future__ import generators
import py import py
from py.__.test.config import gettopdir from py.__.test.config import gettopdir
import suptest, setupdata from py.__.test.testing import suptest
from py.__.test import event from py.__.test import event
def getcolitems(config): def getcolitems(config):
@ -38,6 +38,8 @@ def test_config_cmdline_options():
def test_config_cmdline_options_only_lowercase(): def test_config_cmdline_options_only_lowercase():
o = py.test.ensuretemp('test_config_cmdline_options_only_lowercase') o = py.test.ensuretemp('test_config_cmdline_options_only_lowercase')
o = o.mkdir("onemore") # neccessary because collection of o.dirpath()
# could see our conftest.py
o.ensure("conftest.py").write(py.code.Source(""" o.ensure("conftest.py").write(py.code.Source("""
import py import py
Option = py.test.config.Option Option = py.test.config.Option
@ -159,12 +161,16 @@ def test_config_rconfig():
assert config.option.gdest == 11 assert config.option.gdest == 11
assert option.gdest == 11 assert option.gdest == 11
class TestSessionAndOptions: class TestSessionAndOptions(suptest.FileCreation):
def setup_class(cls): def exampletestfile(self):
cls.tmproot = py.test.ensuretemp(cls.__name__) return self.makepyfile(file_test="""
def test_one():
assert 42 == 43
def setup_method(self, method): class TestClass(object):
self.tmpdir = self.tmproot.ensure(method.__name__, dir=1) def test_method_one(self):
assert 42 == 43
""")
def test_session_eventlog(self): def test_session_eventlog(self):
eventlog = self.tmpdir.join("test_session_eventlog") eventlog = self.tmpdir.join("test_session_eventlog")
@ -312,7 +318,7 @@ class TestSessionAndOptions:
def test_conflict_options(self): def test_conflict_options(self):
def check_conflict_option(opts): def check_conflict_option(opts):
print "testing if options conflict:", " ".join(opts) print "testing if options conflict:", " ".join(opts)
path = setupdata.getexamplefile("file_test.py") path = self.exampletestfile()
config = py.test.config._reparse(opts + [path]) config = py.test.config._reparse(opts + [path])
py.test.raises((ValueError, SystemExit), """ py.test.raises((ValueError, SystemExit), """
config.initsession() config.initsession()
@ -329,7 +335,7 @@ class TestSessionAndOptions:
def test_implied_options(self): def test_implied_options(self):
def check_implied_option(opts, expr): def check_implied_option(opts, expr):
path = setupdata.getexamplefile("file_test.py") path = self.exampletestfile()
config = py.test.config._reparse(opts + [path]) config = py.test.config._reparse(opts + [path])
session = config.initsession() session = config.initsession()
assert eval(expr, session.config.option.__dict__) assert eval(expr, session.config.option.__dict__)
@ -348,19 +354,19 @@ class TestSessionAndOptions:
passed, skipped, failed = sorter.countoutcomes() passed, skipped, failed = sorter.countoutcomes()
assert failed == 2 assert failed == 2
assert skipped == passed == 0 assert skipped == passed == 0
path = setupdata.getexamplefile("file_test.py") path = self.exampletestfile()
for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'], for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'],
['--tb=long'], ['--fulltrace'], ['--nomagic'], ['--tb=long'], ['--fulltrace'], ['--nomagic'],
['--traceconfig'], ['-v'], ['-v', '-v']): ['--traceconfig'], ['-v'], ['-v', '-v']):
yield runfiletest, opts + [path] yield runfiletest, opts + [path]
def test_is_not_boxed_by_default(self): def test_is_not_boxed_by_default(self):
path = setupdata.getexamplefile("file_test.py") path = self.exampletestfile()
config = py.test.config._reparse([path]) config = py.test.config._reparse([path])
assert not config.option.boxed assert not config.option.boxed
class TestConfigColitems: class TestConfigColitems(suptest.FileCreation):
def setup_class(cls): def setup_class(cls):
cls.tmproot = py.test.ensuretemp(cls.__name__) cls.tmproot = py.test.ensuretemp(cls.__name__)

View File

@ -36,9 +36,12 @@ class TestEventBus:
assert l == [1] assert l == [1]
class TestItemTestReport(object): class TestItemTestReport(suptest.InlineCollection):
def test_toterminal(self): def test_toterminal(self):
sorter = suptest.events_run_example("file_test.py") sorter = suptest.events_from_runsource("""
def test_one():
assert 42 == 43
""")
reports = sorter.get(event.ItemTestReport) reports = sorter.get(event.ItemTestReport)
ev = reports[0] ev = reports[0]
assert ev.failed assert ev.failed
@ -54,5 +57,5 @@ class TestItemTestReport(object):
##assert ev.repr_run.find("AssertionError") != -1 ##assert ev.repr_run.find("AssertionError") != -1
filepath = ev.colitem.fspath filepath = ev.colitem.fspath
#filepath , modpath = ev.itemrepr_path #filepath , modpath = ev.itemrepr_path
assert str(filepath).endswith("file_test.py") assert str(filepath).endswith(".py")
#assert modpath.endswith("file_test.test_one") #assert modpath.endswith("file_test.test_one")

View File

@ -1,30 +0,0 @@
from py.__.test import event
import setupdata, suptest
from py.__.code.testing.test_excinfo import TWMock
class TestItemTestReport(object):
def test_toterminal(self):
sorter = suptest.events_run_example("file_test.py")
reports = sorter.get(event.ItemTestReport)
ev = reports[0]
assert ev.failed
twmock = TWMock()
ev.toterminal(twmock)
assert twmock.lines
twmock = TWMock()
ev.outcome.longrepr = "hello"
ev.toterminal(twmock)
assert twmock.lines[0] == "hello"
assert not twmock.lines[1:]
##assert ev.repr_run.find("AssertionError") != -1
filepath = ev.colitem.fspath
#filepath , modpath = ev.itemrepr_path
assert str(filepath).endswith("file_test.py")
#assert modpath.endswith("file_test.test_one")

View File

@ -1,15 +1,20 @@
import py import py
from py.__.test import event from py.__.test import event
import suptest, setupdata from py.__.test.testing import suptest
def setup_module(mod): def setup_module(mod):
mod.tmpdir = py.test.ensuretemp(mod.__name__) mod.tmpdir = py.test.ensuretemp(mod.__name__)
class TestKeywordSelection: class TestKeywordSelection(suptest.InlineSession):
def test_select_simple(self): def test_select_simple(self):
file_test = self.makepyfile(file_test="""
def test_one(): assert 0
class TestClass(object):
def test_method_one(self):
assert 42 == 43
""")
def check(keyword, name): def check(keyword, name):
sorter = suptest.events_run_example("file_test.py", sorter = self.parse_and_run("-s", "-k", keyword, file_test)
'-s', '-k', keyword)
passed, skipped, failed = sorter.listoutcomes() passed, skipped, failed = sorter.listoutcomes()
assert len(failed) == 1 assert len(failed) == 1
assert failed[0].colitem.name == name assert failed[0].colitem.name == name
@ -20,7 +25,7 @@ class TestKeywordSelection:
yield check, 'TestClass.test', 'test_method_one' yield check, 'TestClass.test', 'test_method_one'
def test_select_extra_keywords(self): def test_select_extra_keywords(self):
o = tmpdir.ensure('test_select_extra_keywords', dir=1) o = self.tmpdir
tfile = o.join('test_select.py').write(py.code.Source(""" tfile = o.join('test_select.py').write(py.code.Source("""
def test_1(): def test_1():
pass pass
@ -46,8 +51,12 @@ class TestKeywordSelection:
assert dlist[0].items[0].name == 'test_1' assert dlist[0].items[0].name == 'test_1'
def test_select_starton(self): def test_select_starton(self):
sorter = suptest.events_run_example("test_threepass.py", threepass = self.makepyfile(test_threepass="""
'-k', "test_two:") def test_one(): assert 1
def test_two(): assert 1
def test_three(): assert 1
""")
sorter = self.parse_and_run("-k", "test_two:", threepass)
passed, skipped, failed = sorter.listoutcomes() passed, skipped, failed = sorter.listoutcomes()
assert len(passed) == 2 assert len(passed) == 2
assert not failed assert not failed