Implement dynamic test discovery and running

This commit is contained in:
Yingtong Li 2017-09-25 11:56:10 +10:00
parent 90865eaaca
commit 506e1c287f
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 78 additions and 8 deletions

View File

@ -14,13 +14,13 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest import TestCase from eos.core.tests import EosTestCase
from eos.base.election import * from eos.base.election import *
from eos.base.workflow import * from eos.base.workflow import *
from eos.core.objects import * from eos.core.objects import *
class ElectionPyTestCase(TestCase): class ElectionTestCase(EosTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
client.drop_database('test') client.drop_database('test')

View File

@ -14,13 +14,19 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest import TestCase
from eos.core.bigint import * from eos.core.bigint import *
from eos.core.bitstring import * from eos.core.bitstring import *
from eos.core.objects import * from eos.core.objects import *
class ObjectPyTestCase(TestCase): class EosTestCase:
@classmethod
def setUpClass(cls):
pass
def assertEqual(self, a, b):
self.impl.assertEqual(a, b)
class ObjectTestCase(EosTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
class Person(TopLevelObject): class Person(TopLevelObject):
@ -43,7 +49,7 @@ class ObjectPyTestCase(TestCase):
def test_serialise(self): def test_serialise(self):
person1 = self.Person(name='John', address='Address 1') person1 = self.Person(name='John', address='Address 1')
expect1 = {'_ver': '0.1', 'name': 'John', 'address': 'Address 1'} expect1 = {'_ver': '0.1', 'name': 'John', 'address': 'Address 1'}
expect1a = {'type': 'eos.core.tests.ObjectPyTestCase.setUpClass.<locals>.Person', 'value': expect1} expect1a = {'type': 'eos.core.tests.ObjectTestCase.setUpClass.<locals>.Person', 'value': expect1}
self.assertEqual(person1.serialise(), expect1) self.assertEqual(person1.serialise(), expect1)
self.assertEqual(EosObject.serialise_and_wrap(person1, self.Person), expect1) self.assertEqual(EosObject.serialise_and_wrap(person1, self.Person), expect1)
@ -52,7 +58,7 @@ class ObjectPyTestCase(TestCase):
#self.assertEqual(EosObject.deserialise_and_unwrap(expect1a), person1) #self.assertEqual(EosObject.deserialise_and_unwrap(expect1a), person1)
self.assertEqual(EosObject.deserialise_and_unwrap(expect1a).serialise(), person1.serialise()) self.assertEqual(EosObject.deserialise_and_unwrap(expect1a).serialise(), person1.serialise())
class BigIntPyTestCase(TestCase): class BigIntTestCase(EosTestCase):
def test_basic(self): def test_basic(self):
bigint1 = BigInt(5) bigint1 = BigInt(5)
bigint2 = BigInt('A', 16) bigint2 = BigInt('A', 16)

64
eos/tests.py Normal file
View File

@ -0,0 +1,64 @@
#!/usr/bin/env python3
# Eos - Verifiable elections
# Copyright © 2017 RunasSudo (Yingtong Li)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest import *
from eos.core.bigint import *
from eos.core.bitstring import *
from eos.core.objects import *
import importlib
import os
import types
test_suite = TestSuite()
# All the TestCase's we dynamically generate inherit from this class
class BaseTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.impl.setUpClass()
@classmethod
def add_method(cls, method):
def call_method(self, *args):
cls.impl.impl = self
func = getattr(cls.impl, method)
return func(*args)
setattr(cls, method, call_method)
# Test discovery
import eos.core.tests
for dirpath, dirnames, filenames in os.walk('eos'):
if dirpath == 'eos':
# Skip this file
continue
if 'tests.py' in filenames:
module = importlib.import_module(dirpath.replace('/', '.') + '.tests')
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, type):
if issubclass(obj, eos.core.tests.EosTestCase):
cls = type(name + 'Impl', (BaseTestCase,), {'impl': obj()})
for method in dir(cls.impl):
if isinstance(getattr(cls.impl, method), types.MethodType) and not hasattr(cls, method):
cls.add_method(method)
if method.startswith('test_'):
test_case = cls(method)
test_suite.addTest(test_case)
TextTestRunner(verbosity=3).run(test_suite)

View File

@ -1,3 +1,3 @@
#!/bin/bash #!/bin/bash
coverage run --source=eos --omit='*/js.py','*/tests.py' -m unittest discover . -vvv || exit 1 coverage run --source=eos --omit='*/js.py','eos/js_tests.py','*/tests.py' -m eos.tests || exit 1
coverage html coverage html