public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/pms-test-suite:master commit in: PMSTestSuite/library/test/, PMSTestSuite/library/
Date: Mon, 23 May 2011 07:38:48 +0000 (UTC)	[thread overview]
Message-ID: <5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3.mgorny@gentoo> (raw)

commit:     5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Mon May 23 07:38:46 2011 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Mon May 23 07:38:46 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/pms-test-suite.git;a=commit;h=5df1c8e9

Support loading test cases.

---
 PMSTestSuite/library/__init__.py                   |   85 +++++++++++++++-----
 PMSTestSuite/library/case.py                       |    7 ++
 PMSTestSuite/library/test/__init__.py              |    5 +-
 .../library/test/{__init__.py => random_test.py}   |    6 +-
 4 files changed, 78 insertions(+), 25 deletions(-)

diff --git a/PMSTestSuite/library/__init__.py b/PMSTestSuite/library/__init__.py
index 40db438..322edbd 100644
--- a/PMSTestSuite/library/__init__.py
+++ b/PMSTestSuite/library/__init__.py
@@ -2,38 +2,81 @@
 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-class TestLibrary(object):
-	""" Base class for a test library. """
-	pass
-
-def load_library(name):
-	"""
-	Try to load a test library <name>. Instiantiate the first TestLibrary
-	subclass found there.
+"""
+>>> l = load_library('test')
+>>> l # doctest: +ELLIPSIS
+<PMSTestSuite.library.test.ExampleLibrary object at ...>
+>>> t = [x for x in l][0]
+>>> t # doctest: +ELLIPSIS
+<PMSTestSuite.library.test.random_test.ExampleCase object at ...>
+"""
 
-	Returns a new TestLibrary subclass instance or raises an ImportError.
+from PMSTestSuite.library.case import TestCase
 
-	>>> load_library('test') # doctest: +ELLIPSIS
-	<PMSTestSuite.library.test.ExampleLibrary object at ...>
+def grabmodule(modname, baseclass):
 	"""
-	# XXX: which exceptions can it raise?
+	Import Python module <modname> and find subclasses of <baseclass> there.
 	
-	modname = 'PMSTestSuite.library.%s' % name
+	Returns an iterator over subclasses or raises one of the following
+	exceptions:
+	- ImportError if module import fails,
+	- TypeError if no matching class is found.
+	"""
 	mod = __import__(modname, {}, {}, ['.'], 0)
-	
+	fail = True
+
 	for k in dir(mod):
 		modvar = getattr(mod, k)
 		# the !issubclass() check is necessary to omit TestLibrary class
 		# imported for the subclass
 		try:
-			if issubclass(modvar, TestLibrary) and \
-					not issubclass(TestLibrary, modvar):
-				cls = modvar
-				break
+			if issubclass(modvar, baseclass) and \
+					not issubclass(baseclass, modvar):
+				fail = False
+				yield modvar
 		except TypeError:
 			pass
-	else:
-		raise ImportError('Unable to find a TestLibrary subclass in %s'
+
+	if fail:
+		raise TypeError('Unable to find a %s subclass in %s'
+				% (baseclass.__name__, modname))
+
+class TestLibrary(object):
+	""" Base class for a test library. """
+
+	def __iter__(self):
+		"""
+		Iterate over all the tests in a test library, loading its modules
+		as necessary. Uses <self.test_names> to get the module names.
+
+		Returns an iterator over TestCase subclass instances or raises one
+		of the following exceptions:
+		- ImportError if submodule import fails,
+		- TypeError if no TestCase subclass is found in a submodule.
+		"""
+		for t in self.test_names:
+			cls = grabmodule('%s.%s' % (self.modname, t), TestCase)
+			for c in cls:
+				yield c()
+
+	def __init__(self, modname):
+		self.modname = modname
+
+def load_library(name):
+	"""
+	Try to load a test library <name>. Instiantiate the first TestLibrary
+	subclass found there.
+
+	Returns a new TestLibrary subclass instance or raises one of the following
+	exceptions:
+	- ImportError if module import fails,
+	- TypeError if no matching class is found.
+	"""
+
+	modname = 'PMSTestSuite.library.%s' % name
+	cls = list(grabmodule(modname, TestLibrary))
+	if len(cls) > 1:
+		raise TypeError('%s ambiguous - more than a single TestLibrary subclass found.'
 				% modname)
 
-	return cls()
+	return cls[0](modname)

diff --git a/PMSTestSuite/library/case.py b/PMSTestSuite/library/case.py
new file mode 100644
index 0000000..a2e60f2
--- /dev/null
+++ b/PMSTestSuite/library/case.py
@@ -0,0 +1,7 @@
+#	vim:fileencoding=utf-8
+# (c) 2011 Michał Górny <mgorny@gentoo.org>
+# Released under the terms of the 2-clause BSD license.
+
+class TestCase(object):
+	""" Base class for a test case. """
+	pass

diff --git a/PMSTestSuite/library/test/__init__.py b/PMSTestSuite/library/test/__init__.py
index 91afc38..9cfbe39 100644
--- a/PMSTestSuite/library/test/__init__.py
+++ b/PMSTestSuite/library/test/__init__.py
@@ -8,4 +8,7 @@ class ExampleLibrary(TestLibrary):
 	"""
 	Absolutely random TestLibrary subclass to test it.
 	"""
-	pass
+
+	test_names=[
+		'random_test'
+	]

diff --git a/PMSTestSuite/library/test/__init__.py b/PMSTestSuite/library/test/random_test.py
similarity index 52%
copy from PMSTestSuite/library/test/__init__.py
copy to PMSTestSuite/library/test/random_test.py
index 91afc38..50acc45 100644
--- a/PMSTestSuite/library/test/__init__.py
+++ b/PMSTestSuite/library/test/random_test.py
@@ -2,10 +2,10 @@
 # (c) 2011 Michał Górny <mgorny@gentoo.org>
 # Released under the terms of the 2-clause BSD license.
 
-from PMSTestSuite.library import TestLibrary
+from PMSTestSuite.library.case import TestCase
 
-class ExampleLibrary(TestLibrary):
+class ExampleCase(TestCase):
 	"""
-	Absolutely random TestLibrary subclass to test it.
+	Absolutely random TestCase subclass to test it.
 	"""
 	pass



             reply	other threads:[~2011-05-23  7:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-23  7:38 Michał Górny [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-05-23  9:51 [gentoo-commits] proj/pms-test-suite:master commit in: PMSTestSuite/library/test/, PMSTestSuite/library/ Michał Górny
2011-05-23  9:51 Michał Górny
2011-05-26  6:35 Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5df1c8e9c28b7beabf523cea81f1aa5f7cc2a3c3.mgorny@gentoo \
    --to=mgorny@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox