2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-10-05 13:26:10 +00:00

add better loop support to common_test.py

Merge from trunk revisions 2976 and 2980

Add better support for looping over a tests[] array to common_test.py:
- class AATest - a base class we can use for all tests, and that will
  probably get more features in the future (for example tempdir
  handling)
- setup_all_tests() - a function that iterates over all classes in the
  given file and calls setup_test_loops() for each of them
- setup_tests_loop() - a function that creates tests based on tests[]
  in the given class. Those tests call the class' _run_test() method for
  each test specified in tests[]  (inspired by setup_regex_tests() ;-)

This means we can get rid of the manually maintained tests list in
test-regex_matches.py and just need to call setup_all_tests() once in
each file.

The patch also adds test-example.py, which is
- a demo of the code added to common_test.py
- a template file that we can copy for future test-*.py

Acked-by: Steve Beattie <steve@nxnw.org>
This commit is contained in:
Christian Boltz
2015-04-13 14:28:48 -07:00
committed by Steve Beattie
parent 52b6aeb04c
commit 9ebb1913bd
2 changed files with 79 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
# ----------------------------------------------------------------------
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
# Copyright (C) 2015 Christian Boltz <apparmor@cboltz.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
@@ -12,8 +13,10 @@
#
# ----------------------------------------------------------------------
import unittest
import inspect
import os
import re
import sys
import apparmor.common
import apparmor.config
@@ -34,6 +37,10 @@ class Test(unittest.TestCase):
# print("Please press the Y button on the keyboard.")
# self.assertEqual(apparmor.common.readkey().lower(), 'y', 'Error reading key from shell!')
class AATest(unittest.TestCase):
tests = []
class AAParseTest(unittest.TestCase):
parse_function = None
@@ -44,6 +51,33 @@ class AAParseTest(unittest.TestCase):
'parse object %s returned "%s", expected "%s"' \
%(self.parse_function.__doc__, parsed.serialize(), rule))
def setup_all_tests():
'''call setup_tests_loop() for each class in module_name'''
for name, obj in inspect.getmembers(sys.modules['__main__']):
if inspect.isclass(obj):
if issubclass(obj, unittest.TestCase):
setup_tests_loop(obj)
def setup_tests_loop(test_class):
'''Create tests in test_class using test_class.tests and self._run_test()
test_class.tests should be tuples of (test_data, expected_results)
test_data and expected_results can be of any type as long as test_class._run_test()
know how to handle them.
A typical definition for _run_test() is:
def test_class._run_test(self, test_data, expected)
'''
for (i, (test_data, expected)) in enumerate(test_class.tests):
def stub_test(self, test_data=test_data, expected=expected):
self._run_test(test_data, expected)
stub_test.__doc__ = "test '%s'" % (test_data)
setattr(test_class, 'test_%d' % (i), stub_test)
def setup_regex_tests(test_class):
'''Create tests in test_class using test_class.tests and AAParseTest._test_parse_rule()