From 03c62a64743e85827428784d099fc72944145c6b Mon Sep 17 00:00:00 2001 From: Nikola Dipanov Date: Tue, 29 Apr 2014 11:36:59 +0200 Subject: [PATCH] Add TestCase assertion methods for testing checks This patch adds assertCheckPasses and assertCheckFails methods to make it easier to write tests for more complicated checks that we want to add. We rely on checks being generators at this point, meaning this will not work on flake8 class based plugins. Change-Id: I37ecec271006084211e37c4fc9c89d71df01dc97 --- hacking/tests/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hacking/tests/__init__.py b/hacking/tests/__init__.py index 61925fe1..5061b082 100644 --- a/hacking/tests/__init__.py +++ b/hacking/tests/__init__.py @@ -43,3 +43,16 @@ class TestCase(testtools.TestCase): if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES: stderr = self.useFixture(fixtures.StringStream('stderr')).stream self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) + + def assertCheckFails(self, check_func, *args, **kwargs): + if not list(check_func(*args, **kwargs)): + raise AssertionError("Check %s did not fail." % + check_func.__name__) + + def assertCheckPasses(self, check_func, *args, **kwargs): + try: + self.assertCheckFails(check_func, *args, **kwargs) + except AssertionError: + return + else: + raise AssertionError("Check %s failed." % check_func.__name__)