Created
September 29, 2015 22:50
-
-
Save nicoddemus/52b7c5a1599b9f2b73e7 to your computer and use it in GitHub Desktop.
load_tests example fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
import unittest | |
from _pytest.unittest import TestCaseFunction | |
import pytest | |
class LoadTestsSuiteCollector(pytest.Collector): | |
def __init__(self, name, parent, suite): | |
super(LoadTestsSuiteCollector, self).__init__(name, parent=parent) | |
self.suite = suite | |
self.obj = suite | |
def collect(self): | |
return [LoadTestsCase(case.id(), self, case) | |
for case in self.suite] | |
def reportinfo(self): | |
return str(self.suite) | |
# Using pytest.Function directly causes inexplicable internal py.test errors. | |
class LoadTestsCase(pytest.Function): | |
def __init__(self, name, parent, item): | |
super(LoadTestsCase, self).__init__(name, parent, callobj=self._item_run) | |
self.item = item | |
def _item_run(self): | |
result = self.item() | |
if result.failures: | |
test_method, exception = result.failures[0] | |
pytest.fail(exception) | |
def pytest_pycollect_makeitem(collector, name, obj): | |
if name == 'load_tests' and isinstance(obj, types.FunctionType): | |
suite = unittest.TestSuite() | |
loader = unittest.TestLoader() | |
pattern = '*' | |
try: | |
# Check that the 'load_tests' object is actually a callable that actually | |
# accepts the arguments expected for the load_tests protocol. | |
suite = obj(loader, suite, pattern) | |
except Exception as e: | |
return None | |
else: | |
return LoadTestsSuiteCollector(name, collector, suite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment