changeset 2821:fdf1a03eaf69

tests: convert lock and pysupport tests to py.test
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 10 Sep 2007 21:18:06 +0200
parents 97645f8f7747
children 718a5c4a801f
files MoinMoin/util/_tests/test_lock.py MoinMoin/util/_tests/test_pysupport.py
diffstat 2 files changed, 33 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/_tests/test_lock.py	Mon Sep 10 20:59:49 2007 +0200
+++ b/MoinMoin/util/_tests/test_lock.py	Mon Sep 10 21:18:06 2007 +0200
@@ -2,28 +2,27 @@
 """
     MoinMoin - MoinMoin.module_tested Tests
 
-    Module names must start with 'test_' to be included in the tests.
-
-    @copyright: 2005 by Florian Festi
+    @copyright: 2005 by Florian Festi,
+                2007 by MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
-import unittest, tempfile, os, time, shutil # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
+import tempfile, os, time, shutil
 
 import py
 
 from MoinMoin.util.lock import ExclusiveLock
 
 
-class TestExclusiveLock(unittest.TestCase):
+class TestExclusiveLock(object):
 
-    def setUp(self):
+    def setup_method(self, method):
         self.test_dir = tempfile.mkdtemp('', 'lock_')
         self.test_dir_mtime_goal = time.time()
         self.test_dir_mtime_reported = os.stat(self.test_dir).st_mtime
         self.lock_dir = os.path.join(self.test_dir, "lock")
 
-    def tearDown(self):
+    def teardown_method(self, method):
         shutil.rmtree(self.test_dir)
 
     def testBrokenTimeAPI(self):
@@ -40,17 +39,16 @@
             summer.
         """
         diff = self.test_dir_mtime_reported - self.test_dir_mtime_goal # diff should be 0 or near 0
-        self.failUnless(abs(diff) <= 2,
-                        "Operating System time.time / os.stat inconsistent (diff==%.1fs). Locking will NOT work correctly." % float(diff))
+        assert abs(diff) <= 2
 
     def testTimeout(self):
         """ util.lock: ExclusiveLock: raise ValueError for timeout < 2.0 """
-        self.assertRaises(ValueError, ExclusiveLock, self.lock_dir, timeout=1.0)
+        py.test.raises(ValueError, ExclusiveLock, self.lock_dir, timeout=1.0)
 
     def testAcquire(self):
         """ util.lock: ExclusiveLock: acquire """
         lock = ExclusiveLock(self.lock_dir)
-        self.failUnless(lock.acquire(0.1), "Could not acquire lock")
+        assert lock.acquire(0.1)
 
     def testRelease(self):
         """ util.lock: ExclusiveLock: release
@@ -61,24 +59,23 @@
         if not lock.acquire(0.1):
             py.test.skip("can't acquire lock")
         lock.release()
-        self.failUnless(lock.acquire(0.1),
-                        "Could not acquire lock after release")
+        assert lock.acquire(0.1)
 
     def testIsLocked(self):
         """ util.lock: ExclusiveLock: isLocked """
         lock = ExclusiveLock(self.lock_dir)
         if not lock.acquire(0.1):
             py.test.skip("can't acquire lock")
-        self.failUnless(lock.isLocked(), "lock state wrong")
+        assert lock.isLocked()
         lock.release()
-        self.failIf(lock.isLocked(), "lock state wrong")
+        assert not lock.isLocked()
 
     def testExists(self):
         """ util.lock: ExclusiveLock: exists """
         lock = ExclusiveLock(self.lock_dir)
         if not lock.acquire(0.1):
             py.test.skip("can't acquire lock")
-        self.failUnless(lock.exists(), "lock should exists")
+        assert lock.exists()
 
     def testIsExpired(self):
         """ util.lock: ExclusiveLock: isExpired """
@@ -86,9 +83,9 @@
         lock = ExclusiveLock(self.lock_dir, timeout=timeout)
         if not lock.acquire(0.1):
             py.test.skip("can't acquire lock")
-        self.failIf(lock.isExpired(), "lock should not be expired yet")
+        assert not lock.isExpired()
         time.sleep(timeout)
-        self.failUnless(lock.isExpired(), "lock should be expired")
+        assert lock.isExpired()
 
     def testExpire(self):
         """ util.lock: ExclusiveLock: expire """
@@ -96,9 +93,9 @@
         lock = ExclusiveLock(self.lock_dir, timeout=timeout)
         if not lock.acquire(0.1):
             py.test.skip("can't acquire lock")
-        self.failIf(lock.expire(), "lock should not be expired yet")
+        assert not lock.expire()
         time.sleep(timeout)
-        self.failUnless(lock.expire(), "lock should be expired")
+        assert lock.expire()
 
     def testExclusive(self):
         """ util.lock: ExclusiveLock: lock is exclusive """
@@ -106,7 +103,7 @@
         second = ExclusiveLock(self.lock_dir)
         if not first.acquire(0.1):
             py.test.skip("can't acquire lock")
-        self.failIf(second.acquire(0.1), "first lock is not exclusive")
+        assert not second.acquire(0.1)
 
     def testAcquireAfterTimeout(self):
         """ util.lock: ExclusiveLock: acquire after timeout
@@ -121,8 +118,7 @@
         if second.acquire(0.1):
             py.test.skip("first lock is not exclusive")
         # Second lock should be acquired after timeout
-        self.failUnless(second.acquire(timeout + 0.1),
-                        "can't acquire after timeout")
+        assert second.acquire(timeout + 0.1)
 
     def unlock(self, lock, delay):
         time.sleep(delay)
--- a/MoinMoin/util/_tests/test_pysupport.py	Mon Sep 10 20:59:49 2007 +0200
+++ b/MoinMoin/util/_tests/test_pysupport.py	Mon Sep 10 21:18:06 2007 +0200
@@ -2,18 +2,19 @@
 """
     MoinMoin - MoinMoin.util.pysupport Tests
 
-    @copyright: 2004 Oliver Graf <ograf@bitart.de>
+    @copyright: 2004 Oliver Graf <ograf@bitart.de>,
+                2007 MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details.
 """
 
-import unittest, os, errno # LEGACY UNITTEST, PLEASE DO NOT IMPORT unittest IN NEW TESTS, PLEASE CONSULT THE py.test DOCS
+import os, errno
 
 import py
 
 from MoinMoin.util import pysupport
 
 
-class TestImportNameFromMoin(unittest.TestCase):
+class TestImportNameFromMoin(object):
     """ Test importName of MoinMoin modules
 
     We don't make any testing for files, assuming that moin package is
@@ -22,27 +23,27 @@
 
     def testNonExistingModule(self):
         """ pysupport: import nonexistent module raises ImportError """
-        self.assertRaises(ImportError, pysupport.importName,
-                          'MoinMoin.parser.abcdefghijkl', 'Parser')
+        py.test.raises(ImportError, pysupport.importName,
+                       'MoinMoin.parser.abcdefghijkl', 'Parser')
 
     def testNonExistingAttribute(self):
         """ pysupport: import nonexistent attritbue raises AttributeError """
-        self.assertRaises(AttributeError, pysupport.importName,
-                          'MoinMoin.parser.text_moin_wiki', 'NoSuchParser')
+        py.test.raises(AttributeError, pysupport.importName,
+                       'MoinMoin.parser.text_moin_wiki', 'NoSuchParser')
 
     def testExisting(self):
         """ pysupport: import name from existing module """
         from MoinMoin.parser import text_moin_wiki
         Parser = pysupport.importName('MoinMoin.parser.text_moin_wiki', 'Parser')
-        self.failUnless(Parser is text_moin_wiki.Parser)
+        assert Parser is text_moin_wiki.Parser
 
 
-class TestImportNameFromPlugin(unittest.TestCase):
+class TestImportNameFromPlugin(object):
     """ Base class for import plugin tests """
 
     name = 'Parser'
 
-    def setUp(self):
+    def setup_method(self, method):
         """ Check for valid plugin package """
         self.pluginDirectory = os.path.join(self.request.cfg.data_dir, 'plugin', 'parser')
         self.checkPackage(self.pluginDirectory)
@@ -69,8 +70,8 @@
         """ pysupport: import nonexistent wiki plugin fail """
         if self.pluginExists():
             py.test.skip('plugin exists: %s' % self.plugin)
-        self.assertRaises(ImportError, pysupport.importName,
-                          self.pluginModule, self.name)
+        py.test.raises(ImportError, pysupport.importName,
+                       self.pluginModule, self.name)
 
 
 class TestImportExisting(TestImportNameFromPlugin):
@@ -90,8 +91,7 @@
         try:
             self.createTestPlugin()
             plugin = pysupport.importName(self.pluginModule, self.name)
-            self.assertEqual(getattr(plugin, '__name__', None), self.name,
-                            'Failed to import the test plugin')
+            assert getattr(plugin, '__name__', None) == self.name
         finally:
             self.deleteTestPlugin()