Add a test framework for testing elements.

Change-Id: I845ac3ec6bbcd212ee43151981a2bc5264f4a5b3
This commit is contained in:
Robert Collins 2012-12-14 13:25:18 +13:00
parent 573ee28fd2
commit 1f39f4c629
3 changed files with 85 additions and 0 deletions

View File

@ -139,6 +139,23 @@ comma-delimited string. Some examples:
* break=after-first-boot,before-pre-install will break after the first-boot
hooks and before the pre-install hooks.
Testing Elements
----------------
Elements can be tested using python. To create a test:
* Create a directory called 'tests' in the element directory.
* Create an empty file called '\_\_init\_\_.py' to make it into a python
package.
* Create your test files as 'test\_whatever.py', using regular python test
code.
To run all the tests use testr - `testr run`. To run just some tests provide
one or more regex filters - tests matching any of them are run -
`testr run apt-proxy`.
Third party elements
--------------------

View File

@ -0,0 +1,18 @@
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# conceptually load_tests should be here, but see
# http://bugs.python.org/issue16662 instead, its in test_elements.py

View File

@ -0,0 +1,50 @@
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
import unittest
class StubPackage: pass
# conceptually load_tests should be in __init__, but see
# http://bugs.python.org/issue16662 instead. So, its here in test_elements.py
def load_tests(loader, tests, pattern):
"""load tests for diskimage_builder elements."""
if pattern is None:
# http://bugs.python.org/issue11218
pattern = "test*.py"
this_dir = os.path.dirname(__file__)
elements_dir = os.path.join(this_dir, "..", "elements")
# Make a fake elements top level package, as discovery doesn't let us
# override the python path.
package = StubPackage()
package.__path__ = [elements_dir]
sys.modules['elements'] = package
elements = os.listdir(elements_dir)
for element in elements:
element_dir = os.path.join(elements_dir, element)
tests_path = os.path.join(element_dir, "tests")
if (not os.path.isdir(tests_path) or
not os.path.isfile(os.path.join(tests_path, '__init__.py'))):
continue
# Create a 'package' for the element, so it can be imported.
package = StubPackage()
package.__path__ = [element_dir]
sys.modules['elements.%s' % element] = package
# Try importing the test module
package_tests = loader.discover(start_dir=tests_path, pattern=pattern)
tests.addTests(package_tests)
return tests