[elbe-devel] [PATCH v2 2/5] Creation of "elbepack/junit.py".
Torben Hohn
torben.hohn at linutronix.de
Wed Aug 7 17:14:21 CEST 2019
From: Olivier Dion <dion at linutronix.de>
This is a wrapper around the 'junit_xml' package.
* TestSuite
Wrapper around 'junit_xml.TestSuite'.
** __init__
A TestSuite needs a node and a target filesystem.
** __call__
Calling the TestSuite will automatically iterate over its children
nodes, execute them and return a 'junit_xml.TestSuite' object.
** Other methods
Other methods are private and not meant to be use.
* BaseTest
This is the base class that all tests should inherit from. All
daugther classes from BaseTest should overload the __call__ method.
The __call__ method should return a 'junit_xml.TestCase' object.
It's also important to register the daugther classes to the
TestSuite class. The registration's name should reflect the actual
<test-case> option's name.
Finally, classes that inherit from BaseTest have access to the
self.node, self.target and self.tag attributes.
* Example
This is an hypotical test that would execute a shell script on the
filesystem and assert that the return exit code value of the script
is 0.
For the sake of the example, let's assume that the tag's name for
the <test-case> choice is <script>.
--------------------------------------------------------------------
@TestSuite.register("script")
class TestScript(BaseTest):
def __call__(self):
path = self.node.et.text
test = junit.TestCase(name=path, classname=self.tag)
with self.target:
p = Popen(path, stdout=PIPE, stderr=STDOUT, shell=True)
out, _ = p.communicate()
if p.returncode:
msg = "[%s] - %s" % (errno.errocode[p.returncode],
os.strerror(p.returncode))
test.add_failure_info(message=msg, output=out)
return test
--------------------------------------------------------------------
Signed-off-by: Olivier Dion <dion at linutronix.de>
Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>
---
elbepack/junit.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
create mode 100644 elbepack/junit.py
diff --git a/elbepack/junit.py b/elbepack/junit.py
new file mode 100644
index 000000000..ab7593324
--- /dev/null
+++ b/elbepack/junit.py
@@ -0,0 +1,76 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2019 Olivier Dion <dion at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+
+import junit_xml as junit
+
+
+class TestException(Exception):
+ pass
+
+
+class TestSuite(object):
+
+ test_dict = {}
+
+ def __init__(self, node, target):
+ super(TestSuite, self).__init__()
+ self.node = node
+ self.target = target
+
+ @staticmethod
+ def to_file(output, tss):
+ with open(output, 'w') as f:
+ junit.TestSuite.to_file(f, tss, prettyprint=True)
+
+ @classmethod
+ def register(cls, tag, register=True):
+ def _register(test):
+ test.tag = tag
+ if register is True:
+ cls.test_dict[tag] = test
+ return test
+ return _register
+
+ def do_test(self, node, target):
+ if node.tag not in self.test_dict:
+ raise TestException("Invalid Test %s" % node.tag)
+ test = self.test_dict[node.tag]
+ return test(node, target)()
+
+ def __call__(self):
+ test_cases = []
+ for test in self.node:
+ try:
+ test_cases.append(self.do_test(test, self.target))
+ except TestException as E:
+ pass # TODO - Handle me!
+ ts = junit.TestSuite(name=self.node.et.attrib["name"],
+ test_cases=test_cases)
+ return ts
+
+
+#pylint: disable=too-few-public-methods,no-member
+ at TestSuite.register("BaseTest", register=False)
+class BaseTest(object):
+
+ def __init__(self, node, target):
+ super(BaseTest, self).__init__()
+ self.node = node
+ self.target = target
+
+ def __call__(self):
+ raise TestException("Unimplemented Test %s" % self.tag)
+
+
+ at TestSuite.register("file-exists")
+class TestFileExists(BaseTest):
+
+ def __call__(self):
+ path = self.node.et.text
+ test = junit.TestCase(name=path, classname=self.tag)
+ if not self.target.exists(path):
+ test.add_failure_info(message="FAILED")
+ return test
--
2.11.0
More information about the elbe-devel
mailing list