Skip to content

Instantly share code, notes, and snippets.

@xZise
Last active October 14, 2015 19:58
Show Gist options
  • Save xZise/4f9e70910b020e2664ac to your computer and use it in GitHub Desktop.
Save xZise/4f9e70910b020e2664ac to your computer and use it in GitHub Desktop.
Testing green
# -*- coding: utf-8 -*-
"""
Test aspects to allow fine grained control over what tests are executed.
Several parts of the test infrastructure are implemented as mixins,
such as API result caching and excessive test durations. An unused
mixin to show cache usage is included.
"""
#
# (C) Pywikibot team, 2014-2015
#
# Distributed under the terms of the MIT license.
#
from __future__ import absolute_import, print_function, unicode_literals
__version__ = '$Id: 8e94e3866ed23c6d275740921cd7812aeebcfa87 $'
import sys
import unittest
if sys.version_info[0] > 2:
import six
def add_metaclass(cls):
"""Call six's add_metaclass with the site's __metaclass__ in Python 3."""
if sys.version_info[0] > 2:
return six.add_metaclass(cls.__metaclass__)(cls)
else:
assert cls.__metaclass__
return cls
class MetaTestCaseClass(type):
"""Test meta class."""
def __new__(cls, name, bases, dct):
"""Create the new class."""
tests = [attr_name
for attr_name in dct
if attr_name.startswith('test')]
base_tests = []
if not tests:
for base in bases:
base_tests += [attr_name
for attr_name, attr in base.__dict__.items()
if attr_name.startswith('test') and callable(attr)]
dct['abstract_class'] = not tests and not base_tests
# Bail out if it is the abstract class.
if not dct['abstract_class'] and 'net' in dct:
del dct['net']
return super(MetaTestCaseClass, cls).__new__(cls, name, bases, dct)
@add_metaclass
class TestCase(unittest.TestCase):
"""Run tests on pre-defined sites."""
__metaclass__ = MetaTestCaseClass
def _addUnexpectedSuccess(self, result):
"""Report and ignore."""
print(' unexpected success ', end='')
sys.stdout.flush()
result.addSuccess(self)
def _addExpectedFailure(self, result, exc_info=None):
"""Report and ignore."""
print(' expected failure ', end='')
sys.stdout.flush()
result.addSuccess(self)
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Test tools package alone which don't fit into other tests."""
#
# (C) Pywikibot team, 2015
#
# Distributed under the terms of the MIT license.
from __future__ import absolute_import, unicode_literals
__version__ = '$Id: 919f28cd3b4911113dc58adc08634763f586827e $'
import inspect
import warnings
from aspects import unittest, TestCase, MetaTestCaseClass, add_metaclass
class MetaTestArgSpec(MetaTestCaseClass):
"""Metaclass to create dynamically the tests. Set the net flag to false."""
def __new__(cls, name, bases, dct):
"""Create a new test case class."""
def create_test(method):
def test_method(self):
pass
return test_method
def t(self):
self.assertTrue(True)
for name, tested_method in list(dct.items()):
if name.startswith('_method_test_'):
suffix = name[len('_method_test_'):]
test_name = 'test_method_' + suffix
dct[test_name] = create_test(tested_method)
dct[test_name].__doc__ = 'Test getargspec on {0}'.format(suffix)
# Uncomment these two and it works
#dct['test_example'] = t
#dct['test_example'].__doc__ = 'foobar'
dct['net'] = False
return super(MetaTestArgSpec, cls).__new__(cls, name, bases, dct)
@add_metaclass
class TestArgSpec(TestCase):
"""Test getargspec and ArgSpec from tools."""
__metaclass__ = MetaTestArgSpec
def _method_test_vars(self, param, *args, **kwargs):
"""Test method with two positional arguments and both var args."""
return (['self', 'param'], 'args', 'kwargs', None)
class TestPythonArgSpec(TestArgSpec):
"""Test the same tests using Python's implementation."""
if __name__ == '__main__':
try:
unittest.main()
except SystemExit:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment