Skip to content

Commit

Permalink
docs: add more information for unittests
Browse files Browse the repository at this point in the history
Co-authored-by: Philipp Storz <philipp.storz@bareos.com>
  • Loading branch information
franku and pstorz committed Jan 19, 2021
1 parent 41a0e81 commit 191e240
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
12 changes: 11 additions & 1 deletion core/src/plugins/filed/python/CMakeLists.txt
@@ -1,6 +1,6 @@
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2020-2020 Bareos GmbH & Co. KG
# Copyright (C) 2020-2021 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -161,6 +161,16 @@ if(Python3_FOUND)
"PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}/python3modules:${CMAKE_CURRENT_SOURCE_DIR}/test"
LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/../../../lib
)
add_test(
NAME python-simple-test-example
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/test/simple-test-example.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
set_property(
# add current directory for documentation only
TEST python-simple-test-example PROPERTY ENVIRONMENT PYTHONPATH=./
)
set_property(TEST python-simple-test-example PROPERTY DISABLED true)
endif()

set(PYFILES
Expand Down
26 changes: 26 additions & 0 deletions core/src/plugins/filed/python/test/simple-test-example.py
@@ -0,0 +1,26 @@
#!/usr/bin/env python

# BAREOS - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2021-2021 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

from sys import exit

if __name__ == "__main__":
print("--- Hello World ---")
exit(0)
46 changes: 41 additions & 5 deletions docs/manuals/source/DeveloperGuide/tests.rst
Expand Up @@ -4,10 +4,46 @@ Tests
Unit Tests
----------
Bareos unit tests are usually written in C++ using Google Test.
The unit tests reside in ``core/src/tests`` and are compiled with the rest of Bareos if Google Test is available on your system.
The unit tests reside in ``core/src/tests``. If Google Test is available on your system, the tests are compiled during the normal build process of Bareos.
Unit tests can be run using ``make test`` or ``ctest``.

Adding a new Test
~~~~~~~~~~~~~~~~~
To add a new test, you create your sourcefiles in ``core/src/tests`` and register the test in ``CMakeLists.txt`` in that directory.
There is also a helper script ``add_new_unit_test.sh`` that will setup a test from a template and register it in ``CMakeLists.txt``.
There are many theoretical approaches how to write unit tests. However, in general we use unit tests for software components such as classes and functions as well as for simple integration tests. A unit test should follow the F.I.R.S.T. principle (Fast, Independent, Repeatable, Self-Validating, Timely).

Adding a new C++ Test
~~~~~~~~~~~~~~~~~~~~~
To add a new test, you create your sourcefiles in ``core/src/tests`` and register the test in ``CMakeLists.txt`` in that directory. The easiest way is to copy an existing test sourcefile and the related lines in ``core/src/CMakeLists.txt``.

For general advice on how to use the Google Test framework see this documentation: `Googletest Primer <https://github.com/google/googletest/blob/master/docs/primer.md>`_

Adding tests in general
~~~~~~~~~~~~~~~~~~~~~~~
Unittests in other languages i.e. Python can be established using the add_test and set_property commands of cmake. The following cmake code adds a Python script to the test suite. The actual test in the source is disabled by default.

.. code-block:: shell-session
:caption: core/src/plugins/filed/python/CMakeLists.txt
add_test(
NAME python-simple-test-example
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/test/simple-test-example.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
)
set_property(
#add current directory for documentation only
TEST python-simple-test-example PROPERTY ENVIRONMENT PYTHONPATH=./
)
set_property(
TEST python-simple-test-example PROPERTY DISABLED true
)
.. code-block:: shell-session
:caption: core/src/plugins/filed/python/test/simple-test-example.py
#!/usr/bin/env python
from sys import exit
if __name__ == "__main__":
print("--- Hello World ---")
exit(0)
In this case only the return value of the script is evaluated: 0 for success and 1 for failure.

0 comments on commit 191e240

Please sign in to comment.