[py-svn] r37465 - in py/trunk/py/test: . rsession terminal testing

hpk at codespeak.net hpk at codespeak.net
Sun Jan 28 14:25:07 CET 2007


Author: hpk
Date: Sun Jan 28 14:25:03 2007
New Revision: 37465

Added:
   py/trunk/py/test/testing/test_remote.py
      - copied, changed from r37463, py/trunk/py/test/testing/test_session.py
Modified:
   py/trunk/py/test/config.py
   py/trunk/py/test/rsession/rsession.py
   py/trunk/py/test/session.py
   py/trunk/py/test/terminal/remote.py
   py/trunk/py/test/testing/setupdata.py
   py/trunk/py/test/testing/test_config.py
   py/trunk/py/test/testing/test_session.py
Log:
* all Sessions now have fixoptions()
* added some tests for implied and conflicting options 
* make all Session objects inherit from the base session
* small cleanups with respect to test setup
* separate tests a bit  



Modified: py/trunk/py/test/config.py
==============================================================================
--- py/trunk/py/test/config.py	(original)
+++ py/trunk/py/test/config.py	Sun Jan 28 14:25:03 2007
@@ -48,7 +48,6 @@
         args = [str(x) for x in args]
         cmdlineoption, args = self._parser.parse_args(args) 
         self.option.__dict__.update(vars(cmdlineoption))
-        fixoptions(self.option)  # XXX fixing should be moved to sessions
         if not args:
             args.append(py.std.os.getcwd())
         self.topdir = gettopdir(args)
@@ -130,9 +129,7 @@
         """ return an initialized session object. """
         cls = self._getsessionclass()
         session = cls(self)
-        # XXX: all sessions should have one
-        if hasattr(session, 'fixoptions'):
-            session.fixoptions()
+        session.fixoptions()
         return session
 
     def _getsessionclass(self): 
@@ -272,28 +269,6 @@
     except ValueError:
         raise ValueError("%s=%r is not marshallable" %(name, value))
 
-def fixoptions(option):
-    """ sanity checks and making option values canonical. """
-
-    # implied options
-    if option.usepdb:
-        if not option.nocapture:
-            #print "--pdb implies --nocapture"
-            option.nocapture = True
-
-    if option.runbrowser and not option.startserver:
-        #print "--runbrowser implies --startserver"
-        option.startserver = True
-
-    # conflicting options 
-    if option.looponfailing and option.usepdb:
-        raise ValueError, "--looponfailing together with --pdb not supported."
-    if option.looponfailing and option.dist:
-        raise ValueError, "--looponfailing together with --dist not supported."
-    if option.executable and option.usepdb:
-        raise ValueError, "--exec together with --pdb not supported."
-
-
 def gettopdir(args): 
     """ return the top directory for the given paths.
         if the common base dir resides in a python package 

Modified: py/trunk/py/test/rsession/rsession.py
==============================================================================
--- py/trunk/py/test/rsession/rsession.py	(original)
+++ py/trunk/py/test/rsession/rsession.py	Sun Jan 28 14:25:03 2007
@@ -16,15 +16,23 @@
 from py.__.test.rsession.local import local_loop, plain_runner, apigen_runner,\
     box_runner
 from py.__.test.rsession.reporter import LocalReporter, RemoteReporter
+from py.__.test.session import Session
 
-class AbstractSession(object):
+class AbstractSession(Session): 
     """
         An abstract session executes collectors/items through a runner. 
 
     """
     def __init__(self, config, optimise_localhost=True):
-        self.config = config
+        super(AbstractSession, self).__init__(config=config)
         self.optimise_localhost = optimise_localhost
+
+    def fixoptions(self):
+        option = self.config.option 
+        if option.runbrowser and not option.startserver:
+            #print "--runbrowser implies --startserver"
+            option.startserver = True
+        super(AbstractSession, self).fixoptions()
         
     def getpkgdir(path):
         path = py.path.local(path)
@@ -122,6 +130,7 @@
     """ Remote version of session
     """
     def fixoptions(self):
+        super(RSession, self).fixoptions()
         config = self.config
         try:
             config.getvalue('disthosts')

Modified: py/trunk/py/test/session.py
==============================================================================
--- py/trunk/py/test/session.py	(original)
+++ py/trunk/py/test/session.py	Sun Jan 28 14:25:03 2007
@@ -23,6 +23,21 @@
         if not self.config.option.nomagic:
             py.magic.revoke(assertion=1)
 
+    def fixoptions(self):
+        """ check, fix and determine conflicting options. """
+        option = self.config.option
+        # implied options
+        if option.usepdb:
+            if not option.nocapture:
+                option.nocapture = True
+        # conflicting options
+        if option.looponfailing and option.usepdb:
+            raise ValueError, "--looponfailing together with --pdb not supported."
+        if option.looponfailing and option.dist:
+            raise ValueError, "--looponfailing together with --dist not supported."
+        if option.executable and option.usepdb:
+            raise ValueError, "--exec together with --pdb not supported."
+
     def start(self, colitem): 
         """ hook invoked before each colitem.run() invocation. """ 
 

Modified: py/trunk/py/test/terminal/remote.py
==============================================================================
--- py/trunk/py/test/terminal/remote.py	(original)
+++ py/trunk/py/test/terminal/remote.py	Sun Jan 28 14:25:03 2007
@@ -1,7 +1,7 @@
 from __future__ import generators
 import py
+from py.__.test.session import Session
 from py.__.test.terminal.out import getout 
-import sys
 
 def checkpyfilechange(rootdir, statcache={}):
     """ wait until project files are changed. """
@@ -51,9 +51,9 @@
             l.append(current) 
     return l
 
-class RemoteTerminalSession(object):
+class RemoteTerminalSession(Session):
     def __init__(self, config, file=None):
-        self.config = config 
+        super(RemoteTerminalSession, self).__init__(config=config)
         self._setexecutable()
         if file is None:
             file = py.std.sys.stdout 
@@ -123,8 +123,6 @@
     topdir, repr, failures = channel.receive()
     print "SLAVE: received configuration, using topdir:", topdir
     config = py.test.config 
-    import sys
-    sys.stdout.flush()
     config.initdirect(topdir, repr, failures)
     config.option.session = None
     config.option.looponfailing = False 

Modified: py/trunk/py/test/testing/setupdata.py
==============================================================================
--- py/trunk/py/test/testing/setupdata.py	(original)
+++ py/trunk/py/test/testing/setupdata.py	Sun Jan 28 14:25:03 2007
@@ -1,13 +1,20 @@
 import py
 
+def setup_module(mod):
+    mod.datadir = setupdatadir()
+    mod.tmpdir = py.test.ensuretemp(mod.__name__) 
+
 def setupdatadir():
     datadir = py.test.ensuretemp("datadir")
-    if not datadir.listdir():
-        for name, content in namecontent:
+    names = [x.basename for x in datadir.listdir()]
+    for name, content in namecontent:
+        if name not in names:
             datadir.join(name).write(content)
     return datadir
 
 namecontent = [
+('syntax_error.py', "this is really not python\n"),
+
 ('disabled_module.py', py.code.Source('''
         disabled = True
 

Modified: py/trunk/py/test/testing/test_config.py
==============================================================================
--- py/trunk/py/test/testing/test_config.py	(original)
+++ py/trunk/py/test/testing/test_config.py	Sun Jan 28 14:25:03 2007
@@ -231,7 +231,8 @@
 
     def test_sessionname_lookup_custom(self):
         self.tmpdir.join("conftest.py").write(py.code.Source("""
-            class MySession:
+            from py.__.test.session import Session
+            class MySession(Session):
                 def __init__(self, config):
                     self.config = config 
         """)) 
@@ -357,3 +358,4 @@
         col3 = config._getcollector(config.topdir.dirpath())
         py.test.raises(ValueError, 
               "config.get_collector_trail(col3)")
+

Copied: py/trunk/py/test/testing/test_remote.py (from r37463, py/trunk/py/test/testing/test_session.py)
==============================================================================
--- py/trunk/py/test/testing/test_session.py	(original)
+++ py/trunk/py/test/testing/test_remote.py	Sun Jan 28 14:25:03 2007
@@ -1,316 +1,7 @@
 import py
-from setupdata import setupdatadir
-
-def setup_module(mod):
-    mod.datadir = setupdatadir()
-    mod.tmpdir = py.test.ensuretemp(mod.__name__) 
-
-def test_default_session_options():
-    for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'], 
-                 ['--tb=long'], ['--fulltrace'], ['--nomagic'], 
-                 ['--traceconfig'], ['-v'], ['-v', '-v']):
-        yield runfiletest, opts
-
-def runfiletest(opts):
-    config = py.test.config._reparse(opts + [datadir/'filetest.py']) 
-    session = config.initsession()
-    session.main()
-    l = session.getitemoutcomepairs(py.test.Item.Failed)
-    assert len(l) == 2 
-    l = session.getitemoutcomepairs(py.test.Item.Passed)
-    assert not l 
-
-class TestKeywordSelection: 
-    def test_select_simple(self): 
-        for keyword in ['test_one', 'est_on']:
-            config = py.test.config._reparse([datadir/'filetest.py', 
-                                                   '-k', keyword])
-            session = config._getsessionclass()(config, py.std.sys.stdout)
-            session.main()
-            l = session.getitemoutcomepairs(py.test.Item.Failed)
-            assert len(l) == 1 
-            item = l[0][0]
-            assert item.name == 'test_one'
-            l = session.getitemoutcomepairs(py.test.Item.Skipped)
-            assert len(l) == 1 
-
-    def test_select_extra_keywords(self): 
-        o = tmpdir.ensure('selecttest', dir=1)
-        tfile = o.join('test_select.py').write(py.code.Source("""
-            def test_1():
-                pass 
-            class TestClass: 
-                def test_2(self): 
-                    pass
-        """))
-        conftest = o.join('conftest.py').write(py.code.Source("""
-            import py
-            class Class(py.test.collect.Class): 
-                def haskeyword(self, keyword): 
-                    return keyword == 'xxx' or \
-                           super(Class, self).haskeyword(keyword) 
-        """))
-        for keyword in ('xxx', 'xxx test_2', 'TestClass', 'xxx -test_1', 
-                        'TestClass test_2', 'xxx TestClass test_2',): 
-            f = py.std.StringIO.StringIO()
-            config = py.test.config._reparse([o, '-k', keyword]) 
-            session = config._getsessionclass()(config, f) 
-            session.main()
-            print "keyword", repr(keyword)
-            l = session.getitemoutcomepairs(py.test.Item.Passed)
-            assert len(l) == 1
-            assert l[0][0].name == 'test_2'
-            l = session.getitemoutcomepairs(py.test.Item.Skipped)
-            assert l[0][0].name == 'test_1' 
-   
-#f = open('/tmp/logfile', 'wa')  
-class TestTerminalSession: 
-
-    def setup_class(cls):
-        (datadir / 'syntax_error.py').write("\nthis is really not python\n")
-
-    def teardown_class(cls):
-        (datadir / 'syntax_error.py').remove()
-
-    def mainsession(self, *args): 
-        from py.__.test.terminal.terminal import TerminalSession
-        self.file = py.std.StringIO.StringIO() 
-        config = py.test.config._reparse(list(args))
-        session = TerminalSession(config, file=self.file) 
-        session.main()
-        return session
-
-    def test_terminal(self): 
-        session = self.mainsession(datadir / 'filetest.py')
-        out = self.file.getvalue() 
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 2
-        assert out.find('2 failed') != -1 
-
-    def test_syntax_error_module(self): 
-        session = self.mainsession(datadir / 'syntax_error.py')
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 1 
-        out = self.file.getvalue() 
-        assert out.find(str('syntax_error.py')) != -1
-        assert out.find(str('not python')) != -1
-
-    def test_exit_first_problem(self): 
-        session = self.mainsession("--exitfirst", 
-                                   datadir / 'filetest.py')
-        assert session.config.option.exitfirst
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 1 
-        l = session.getitemoutcomepairs(py.test.Item.Passed)
-        assert not l 
-
-    def test_collectonly(self): 
-        session = self.mainsession("--collectonly", 
-                                   datadir / 'filetest.py')
-        assert session.config.option.collectonly
-        out = self.file.getvalue()
-        #print out 
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        #if l: 
-        #    x = l[0][1].excinfo
-        #    print x.exconly() 
-        #    print x.traceback
-        assert len(l) == 0 
-        for line in ('filetest.py', 'test_one', 
-                     'TestClass', 'test_method_one'): 
-            assert out.find(line) 
-
-    def test_recursion_detection(self): 
-        o = tmpdir.ensure('recursiontest', dir=1)
-        tfile = o.join('test_recursion.py')
-        tfile.write(py.code.Source("""
-            def test_1():
-                def f(): 
-                    g() 
-                def g(): 
-                    f() 
-                f() 
-        """))
-        session = self.mainsession(o)
-        print "back from main", o
-        out = self.file.getvalue() 
-        #print out
-        i = out.find('Recursion detected') 
-        assert i != -1 
-
-    def test_generator_yields_None(self): 
-        o = tmpdir.ensure('generatornonetest', dir=1)
-        tfile = o.join('test_generatornone.py')
-        tfile.write(py.code.Source("""
-            def test_1():
-                yield None 
-        """))
-        session = self.mainsession(o) 
-        out = self.file.getvalue() 
-        #print out
-        i = out.find('TypeError') 
-        assert i != -1 
-
-    def test_capturing_hooks_simple(self): 
-        o = tmpdir.ensure('capturing', dir=1)
-        tfile = o.join('test_capturing.py').write(py.code.Source("""
-            import py
-            print "module level output"
-            def test_capturing():
-                print 42
-                print >>py.std.sys.stderr, 23 
-            def test_capturing_error():
-                print 1
-                print >>py.std.sys.stderr, 2
-                raise ValueError
-        """))
-        conftest = o.join('conftest.py').write(py.code.Source("""
-            import py
-            class Function(py.test.Function): 
-                def startcapture(self): 
-                    self._mycapture = py.io.OutErrCapture() 
-                    
-                def finishcapture(self): 
-                    self._testmycapture = self._mycapture.reset()
-        """))
-        session = self.mainsession(o) 
-        l = session.getitemoutcomepairs(py.test.Item.Passed)
-        assert len(l) == 1
-        item = l[0][0]
-        assert hasattr(item, '_testmycapture')
-        print item._testmycapture
-        out, err = item._testmycapture 
-        assert int(out.strip()) == 42
-        assert int(err.strip()) == 23 
-
-        assert isinstance(item.parent, py.test.collect.Module)
-        out, err = item.parent.getouterr()
-        assert out.find('module level output') != -1 
-        allout = self.file.getvalue()
-        print "allout:", allout
-        assert allout.find('module level output') != -1, (
-                           "session didn't show module output")
-
-    def test_raises_output(self): 
-        o = tmpdir.ensure('raisestest', dir=1)
-        tfile = o.join('test_raisesoutput.py')
-        tfile.write(py.code.Source("""
-            import py
-            def test_raises_doesnt():
-                py.test.raises(ValueError, int, "3")
-        """))
-        session = self.mainsession(o) 
-        out = self.file.getvalue() 
-        if not out.find("DID NOT RAISE") != -1: 
-            print out
-            py.test.fail("incorrect raises() output") 
-
-    def test_order_of_execution(self): 
-        o = tmpdir.ensure('ordertest', dir=1)
-        tfile = o.join('test_orderofexecution.py')
-        tfile.write(py.code.Source("""
-            l = []
-            def test_1():
-                l.append(1)
-            def test_2():
-                l.append(2)
-            def test_3():
-                assert l == [1,2]
-            class Testmygroup:
-                reslist = l
-                def test_1(self):
-                    self.reslist.append(1)
-                def test_2(self):
-                    self.reslist.append(2)
-                def test_3(self):
-                    self.reslist.append(3)
-                def test_4(self):
-                    assert self.reslist == [1,2,1,2,3]
-        """))
-
-        session = self.mainsession(o) 
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 0 
-        l = session.getitemoutcomepairs(py.test.Item.Passed)
-        assert len(l) == 7 
-        # also test listnames() here ... 
-        item, result = l[-1]
-        assert item.name == 'test_4' 
-        names = item.listnames()
-        assert names == ['ordertest', 'test_orderofexecution.py', 'Testmygroup', '()', 'test_4']
-
-    def test_nested_import_error(self): 
-        o = tmpdir.ensure('Ians_importfailure', dir=1) 
-        tfile = o.join('test_import_fail.py')
-        tfile.write(py.code.Source("""
-            import import_fails
-            def test_this():
-                assert import_fails.a == 1
-        """))
-        o.join('import_fails.py').write(py.code.Source("""
-            import does_not_work 
-            a = 1
-        """))
-        session = self.mainsession(o) 
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 1 
-        item, outcome = l[0]
-        assert str(outcome.excinfo).find('does_not_work') != -1 
-
-    def test_safe_repr(self):
-        session = self.mainsession(datadir/'brokenrepr.py')
-        out = self.file.getvalue()
-        print 'Output of simulated "py.test brokenrepr.py":'
-        print out
-        
-        l = session.getitemoutcomepairs(py.test.Item.Failed)
-        assert len(l) == 2
-        assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
-        assert out.find("[unknown exception raised in repr()]") != -1
-
-    def test_E_on_correct_line(self):
-        o = tmpdir.ensure('E_on_correct_line', dir=1)
-        tfile = o.join('test_correct_line.py')
-        source = py.code.Source("""
-            import py
-            def test_hello():
-                assert (None ==
-                        ['a',
-                         'b',
-                         'c'])
-        """)
-        tfile.write(source)
-        session = self.mainsession(o) 
-        out = self.file.getvalue()
-        print 'Output of simulated "py.test test_correct_line.py":'
-        print out
-        i = out.find('test_correct_line.py:')
-        assert i >= 0
-        linenum = int(out[i+len('test_correct_line.py:')])  # a single char
-        line_to_report = source[linenum-1]
-        expected_output = '\nE   ' + line_to_report + '\n'
-        print 'Looking for:', expected_output
-        assert expected_output in out
-
+from py.__.test.testing.setupdata import setup_module
 
 class TestRemote: 
-    def XXXtest_rootdir_is_package(self): 
-        d = tmpdir.ensure('rootdirtest1', dir=1) 
-        d.ensure('__init__.py')
-        x1 = d.ensure('subdir', '__init__.py')
-        x2 = d.ensure('subdir2', '__init__.py')
-        x3 = d.ensure('subdir3', 'noinit', '__init__.py')
-        assert getrootdir([x1]) == d 
-        assert getrootdir([x2]) == d 
-        assert getrootdir([x1,x2]) == d 
-        assert getrootdir([x3,x2]) == d 
-        assert getrootdir([x2,x3]) == d 
-
-    def XXXtest_rootdir_is_not_package(self): 
-        one = tmpdir.ensure('rootdirtest1', 'hello') 
-        rootdir = getrootdir([one]) 
-        assert rootdir == one.dirpath() 
-
     def test_exec(self): 
         o = tmpdir.ensure('remote', dir=1) 
         tfile = o.join('test_exec.py')

Modified: py/trunk/py/test/testing/test_session.py
==============================================================================
--- py/trunk/py/test/testing/test_session.py	(original)
+++ py/trunk/py/test/testing/test_session.py	Sun Jan 28 14:25:03 2007
@@ -1,9 +1,38 @@
 import py
-from setupdata import setupdatadir
+from setupdata import setup_module # sets up global 'tmpdir' 
 
-def setup_module(mod):
-    mod.datadir = setupdatadir()
-    mod.tmpdir = py.test.ensuretemp(mod.__name__) 
+implied_options = {
+    '--pdb': 'usepdb and nocapture', 
+    '-v': 'verbose', 
+    '-l': 'showlocals',
+    '--runbrowser': 'startserver and runbrowser', 
+}
+
+conflict_options = ('--looponfailing --pdb',
+                    '--dist --pdb', 
+                    '--exec=%s --pdb' % py.std.sys.executable, 
+                   )
+
+def test_conflict_options():
+    for spec in conflict_options: 
+        opts = spec.split()
+        yield check_conflict_option, opts
+
+def check_conflict_option(opts):
+    print "testing if options conflict:", " ".join(opts)
+    config = py.test.config._reparse(opts + [datadir/'filetest.py'])
+    py.test.raises((ValueError, SystemExit), """
+        config.initsession()
+    """)
+    
+def test_implied_options():
+    for key, expr in implied_options.items():
+        yield check_implied_option, [key], expr
+
+def check_implied_option(opts, expr):
+    config = py.test.config._reparse(opts + [datadir/'filetest.py'])
+    session = config.initsession()
+    assert eval(expr, session.config.option.__dict__)
 
 def test_default_session_options():
     for opts in ([], ['-l'], ['-s'], ['--tb=no'], ['--tb=short'], 
@@ -63,15 +92,7 @@
             l = session.getitemoutcomepairs(py.test.Item.Skipped)
             assert l[0][0].name == 'test_1' 
    
-#f = open('/tmp/logfile', 'wa')  
 class TestTerminalSession: 
-
-    def setup_class(cls):
-        (datadir / 'syntax_error.py').write("\nthis is really not python\n")
-
-    def teardown_class(cls):
-        (datadir / 'syntax_error.py').remove()
-
     def mainsession(self, *args): 
         from py.__.test.terminal.terminal import TerminalSession
         self.file = py.std.StringIO.StringIO() 
@@ -291,72 +312,3 @@
         expected_output = '\nE   ' + line_to_report + '\n'
         print 'Looking for:', expected_output
         assert expected_output in out
-
-
-class TestRemote: 
-    def XXXtest_rootdir_is_package(self): 
-        d = tmpdir.ensure('rootdirtest1', dir=1) 
-        d.ensure('__init__.py')
-        x1 = d.ensure('subdir', '__init__.py')
-        x2 = d.ensure('subdir2', '__init__.py')
-        x3 = d.ensure('subdir3', 'noinit', '__init__.py')
-        assert getrootdir([x1]) == d 
-        assert getrootdir([x2]) == d 
-        assert getrootdir([x1,x2]) == d 
-        assert getrootdir([x3,x2]) == d 
-        assert getrootdir([x2,x3]) == d 
-
-    def XXXtest_rootdir_is_not_package(self): 
-        one = tmpdir.ensure('rootdirtest1', 'hello') 
-        rootdir = getrootdir([one]) 
-        assert rootdir == one.dirpath() 
-
-    def test_exec(self): 
-        o = tmpdir.ensure('remote', dir=1) 
-        tfile = o.join('test_exec.py')
-        tfile.write(py.code.Source("""
-            def test_1():
-                assert 1 == 0 
-        """))
-        print py.std.sys.executable
-        config = py.test.config._reparse(
-                        ['--exec=' + py.std.sys.executable, 
-                         o])
-        cls = config._getsessionclass() 
-        out = []  # out = py.std.Queue.Queue() 
-        session = cls(config, out.append) 
-        session.main()
-        for s in out: 
-            if s.find('1 failed') != -1: 
-                break 
-        else: 
-            py.test.fail("did not see test_1 failure") 
-
-    def test_looponfailing(self): 
-        o = tmpdir.ensure('looponfailing', dir=1) 
-        tfile = o.join('test_looponfailing.py')
-        tfile.write(py.code.Source("""
-            def test_1():
-                assert 1 == 0 
-        """))
-        print py.std.sys.executable
-        config = py.test.config._reparse(['--looponfailing', str(o)])
-        cls = config._getsessionclass() 
-        out = py.std.Queue.Queue() 
-        session = cls(config, out.put) 
-        pool = py._thread.WorkerPool() 
-        reply = pool.dispatch(session.main)
-        while 1: 
-            s = out.get(timeout=1.0)
-            if s.find('1 failed') != -1: 
-                break 
-            print s
-        else: 
-            py.test.fail("did not see test_1 failure") 
-        # XXX we would like to have a cleaner way to finish 
-        try: 
-            reply.get(timeout=0.5) 
-        except IOError, e: 
-            assert str(e).lower().find('timeout') != -1 
-
-        



More information about the pytest-commit mailing list