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

Bastian Germann bage at linutronix.de
Mon May 11 14:36:21 CEST 2020



Am 04.05.20 um 20:00 schrieb dion at linutronix.de (Olivier Dion):
> 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

reflects

> '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 executed 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>

With the typo fix
Reviewed-by: Bastian Germann <bage at linutronix.de>

> ---
>  elbepack/tests/__init__.py     |  0
>  elbepack/tests/test_version.py | 32 ++++++++++++++++++++++++++++++++
>  2 files changed, 32 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..e29681fd
> --- /dev/null
> +++ b/elbepack/tests/test_version.py
> @@ -0,0 +1,32 @@
> +# 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
> +
> +# Since this is just an example on how to make tests, we skip them
> +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
> +
> +    @unittest.skip
> +    def test_version(self):
> +        self.my_list.append(1)
> +        self.assertEqual(elbe_version, self.expected_version)
> +
> +    @unittest.skip
> +    def test_mutable_state(self):
> +        self.assertEqual(len(self.my_list), 0)
> 



More information about the elbe-devel mailing list