[elbe-devel] [RFC PATCH 2/6] tests version: Introduction to Elbe unit testing

Olivier Dion dion at linutronix.de
Tue Apr 7 21:32:42 CEST 2020


From: Torben Hohn <torben.hohn at linutronix.de>

It's not completely clear, that elbepack is the correct module to be
tested, and not the installed version, for example.

Add a Test, that compares the version with the expected one.

This serves as an example on how to create new unit tests.

The steps to follow are:

1)
Create a file prefixed with 'test_' under "elbepack/tests".  The
file's suffix should reflect the name of the module to test.  In this
case, the suffix is 'version' which reflect the module
'elbepack.version'.  Thus, the final file can be found under
"elbepack/tests/test_version.py".

2)
Import the Python's unit testing framework and other standard modules
that are needed.

3)
Import the Elbe module(s) that are needed for testing.

4)
Create classes that inherit from 'unittest.TestCase', with the
prefix 'Test', followed by a short description of what is being
tested.  For example, 'TestElbepackVersion'.

5)
Create methods for each class with the prefix 'test_' for methods that
are to be tested.  Helper and wrapper methods should _not_ have the
'test_' prefix.  Test methods should reflect different cases of what
is being tested in their class.

6)
As a rule of thumb, try to only make a single assertion per test
method.  However, it's fine to put more.

7)
Tests in a class are runned in order according to their name.  Thus,
you should not expect a test to run before or after another one.

8)
Tests can share states.  States that are read-only can be put at the
class level, states that are mutable, or that need to be unique for
every test should be created in the 'setUp' method.

For example, 'expected_version' is a read-only state that can be
shared across tests.  However, 'my_list' is a mutable state that
_must_ be unique.

Don't use '__init__' to set up the tests.  Prefer 'setUp' and
'tearDown'.

9)
For skipping tests, use the '@unittest.skip*' decorators.  For tests
that are expected to fail, use the '@unittest.expectedFailure'
decorator.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
Signed-off-by: Olivier Dion <dion at linutronix.de>
---
 elbepack/tests/__init__.py     |  0
 elbepack/tests/test_version.py | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 elbepack/tests/__init__.py
 create mode 100644 elbepack/tests/test_version.py

diff --git a/elbepack/tests/__init__.py b/elbepack/tests/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/elbepack/tests/test_version.py b/elbepack/tests/test_version.py
new file mode 100644
index 00000000..db0cb6ff
--- /dev/null
+++ b/elbepack/tests/test_version.py
@@ -0,0 +1,29 @@
+# ELBE - Debian Based Embedded Rootfilesystem Builder
+# Copyright (c) 2020 Olivier Dion <dion at linutronix.de>
+# Copyright (c) 2020 Torben Hohn <torbenh at linutronix.de>
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+import unittest
+
+from elbepack.version import elbe_version
+
+class TestElbepackVersion(unittest.TestCase):
+
+    # This is a read-only state that is the same for every tests
+    expected_version = "12.3"
+
+    def setUp(self):
+        # This is a mutable state that is different for every tests
+        self.my_list = []
+
+    def tearDown(self):
+        # You might want to cleanup your mutable states here
+        pass
+
+    def test_version(self):
+        self.my_list.append(1)
+        self.assertEqual(elbe_version, self.expected_version)
+
+    def test_mutable_state(self):
+        self.assertEqual(len(self.my_list), 0)
-- 
2.26.0




More information about the elbe-devel mailing list