From 91685c3ad565abb3c9a3dcc9f43c7390acc8fcf8 Mon Sep 17 00:00:00 2001 From: Andrea Settimi Date: Fri, 16 Aug 2024 15:06:41 +0200 Subject: [PATCH] ADD: testing docu on sphinx docs --- doc/conf.py | 22 ++-- doc/documentation.md | 2 +- doc/testing.rst | 144 +++++++++++++++++---------- tests/unit_tests/DFPointCloudTest.cc | 7 +- 4 files changed, 110 insertions(+), 65 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 4f192637..269b49e8 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -45,17 +45,17 @@ os.add_dll_directory(extra_dll_dir_doc) # For finding DLL dependencies on Windows sys.path.insert(0, extra_dll_dir_doc) sys.path.insert(0, extra_dll_dir_pysource) -# try: -# import diffCheck.diffcheck_bindings as dfb -# except ImportError as e: -# print(f"Failed to import diffcheck_bindings: {e}") -# print("Current sys.path directories:") -# for path in sys.path: -# print(path) -# print("Current files in the directory:") -# for file in os.listdir(extra_dll_dir_doc): -# print(file) -# sys.exit(1) +try: + import diffCheck.diffcheck_bindings as dfb +except ImportError as e: + print(f"Failed to import diffcheck_bindings: {e}") + print("Current sys.path directories:") + for path in sys.path: + print(path) + print("Current files in the directory:") + for file in os.listdir(extra_dll_dir_doc): + print(file) + sys.exit(1) # -- Project information ----------------------------------------------------- diff --git a/doc/documentation.md b/doc/documentation.md index cf27c89a..b9795ef2 100644 --- a/doc/documentation.md +++ b/doc/documentation.md @@ -1,4 +1,4 @@ (doc_guide)= -# Documentation guide +# Documentation /// from CONTRIBUTING how to write documentation \ No newline at end of file diff --git a/doc/testing.rst b/doc/testing.rst index e61c93b9..8fb32cda 100644 --- a/doc/testing.rst +++ b/doc/testing.rst @@ -1,63 +1,103 @@ .. _test_guide: -Testing -======= - -In df we use `CTest` as a test framework managed by Cmake in the file `cmake/tests.cmake` to run: -* [c++](cpp_test) tests with `GoogleTest`, and -* [python](py_test) in `PyTest`. - -Tests are in the `tests` folder, and here's its structure: - -```terminal -F:\DIFFCHECK\TESTS -│ allCppTests.cc -│ -├───integration_tests <-- mainly python interfaces -│ ├───ghcomponents_tests <-- relative to gh components -│ │ .gitkeep -│ │ -│ ├───package_tests <-- relative to the pypi package -│ │ .gitkeep -│ │ -│ └───pybinds_tests <-- strictly pybinding -│ │ diffCheck.dll -│ │ diffcheck_bindings.cp39-win_amd64.pyd -│ │ Open3D.dll -│ │ test_pybind_pyver.py -│ │ test_pybind_units.py -│ -├───test_data <-- here is where we put some .ply data -│ roof_quarter.ply -│ -└───unit_tests <-- c++ backend, one for each header - DFLog.cc - DFPointCloudTest.cc -``` +DFTesting +========= + +In df we use `CTest` as a test framework managed by Cmake in the file ``cmake/tests.cmake`` to run: + +* `c++ <#cpp_test>`_ tests with `GoogleTest`, and +* `Python <#py_test>`_ in `PyTest`. + +Tests are in the ``tests`` folder, and here's its structure: + +.. code-block:: console + + F:\DIFFCHECK\TESTS + │ allCppTests.cc + │ + ├───integration_tests <-- mainly python interfaces + │ ├───ghcomponents_tests <-- relative to gh components + │ │ .gitkeep + │ │ + │ ├───package_tests <-- relative to the pypi package + │ │ .gitkeep + │ │ + │ └───pybinds_tests <-- strictly pybinding + │ │ diffCheck.dll + │ │ diffcheck_bindings.cp39-win_amd64.pyd + │ │ Open3D.dll + │ │ test_pybind_pyver.py + │ │ test_pybind_units.py + │ + ├───test_data <-- here is where we put some .ply data + │ roof_quarter.ply + │ + └───unit_tests <-- c++ backend, one for each header + DFLog.cc + DFPointCloudTest.cc To run the tests, you can use the following commands: -```terminal -cmake -S . -B build -A x64 -DBUILD_PYTHON_MODULE=ON -DBUILD_TESTS=ON -DRUN_TESTS=ON -cmake --build build --config Release -``` - -(py_test)= -## Write Python tests - -To write a test, you need to create a new file in the `tests/integration_tests` folder. Write a new `.py` test file if you are not contributing to an already existing test, and add it in the `cmake/tests.cmake` in the `add_test` function. -e.g.: -```cmake -add_test(NAME PYBIND_UNIT_TEST + +.. code-block:: console + + cmake -S . -B build -A x64 -DBUILD_PYTHON_MODULE=ON -DBUILD_TESTS=ON -DRUN_TESTS=ON + cmake --build build --config Release + + +.. _py_test: + +Write DF Python tests +--------------------- + +To write a test, you need to create a new file in the ``tests/integration_tests`` folder. Write a new ``.py`` test file if you are not contributing to an already existing test, and add it in the ``cmake/tests.cmake`` in the ``add_test()`` function: + +.. code-block:: cmake + + add_test(NAME PYBIND_UNIT_TEST + COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/tests/integration_tests/pybinds_tests/test_pybind_units.py + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) + +Use a fixture to your needs, and write your test. Here is an example of a fixture that always load a point cloud from the ``test_data`` folder: + +.. literalinclude:: ../tests/integration_tests/pybinds_tests/test_pybind_units.py + :language: python + :pyobject: create_DFPointCloudSampleRoof + :caption: `test_pybind_units.py <../tests/integration_tests/pybinds_tests/test_pybind_units.py>`_ + +Than you can use it in your test: + +.. literalinclude:: ../tests/integration_tests/pybinds_tests/test_pybind_units.py + :language: python + :pyobject: test_DFPointCloud_apply_color + + +.. _cpp_test: + +Write DF C++ tests +------------------ + +To write a test, you need to create a new file in the ``tests/unit_tests`` folder. Next add your file in the executable ``${CPP_UNIT_TESTS}`` in the ``cmake/tests.cmake``: + +.. code-block:: cmake + + add_test(NAME PYBIND_UNIT_TEST COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/tests/integration_tests/pybinds_tests/test_pybind_units.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} ) -``` +Use a fixture to your needs, and write your test. Here is an example of a fixture that always load a point cloud from the ``test_data`` folder: + +.. literalinclude:: ../tests/unit_tests/DFPointCloudTest.cc + :language: cpp + :lines: 1-27 + :caption: `DFPointCloudTest.cc <../tests/unit_tests/DFPointCloudTest.cc>`_ + +and you can use it in your test: -(cpp_test)= -## Write C++ tests +.. code-block:: cpp -To write a test, you need to create a new file in the `tests/unit_tests` folder. Next add your file in the executable `${CPP_UNIT_TESTS}` in the `cmake/tests.cmake`. -e.g.: -https://github.com/diffCheckOrg/diffCheck/blob/e080a93cdd73d96efb0686f80bf13730e0b8efa3/cmake/tests.cmake#L13-L17 \ No newline at end of file + TEST_F(DFPointCloudTestFixture, HasColors) { + EXPECT_TRUE(dfPointCloud.HasColors()); + } \ No newline at end of file diff --git a/tests/unit_tests/DFPointCloudTest.cc b/tests/unit_tests/DFPointCloudTest.cc index d33cd2e9..059a27ee 100644 --- a/tests/unit_tests/DFPointCloudTest.cc +++ b/tests/unit_tests/DFPointCloudTest.cc @@ -2,6 +2,10 @@ #include "diffCheck.hh" #include "diffCheck/IOManager.hh" +//------------------------------------------------------------------------- +// fixtures +//------------------------------------------------------------------------- + class DFPointCloudTestFixture : public ::testing::Test { protected: std::vector points; @@ -22,6 +26,8 @@ class DFPointCloudTestFixture : public ::testing::Test { } }; +// add your fixtures here.. + //------------------------------------------------------------------------- // basic constructors //------------------------------------------------------------------------- @@ -51,7 +57,6 @@ TEST_F(DFPointCloudTestFixture, LoadFromPLY) { EXPECT_EQ(dfPointCloud.GetNumPoints(), 7379); EXPECT_EQ(dfPointCloud.GetNumColors(), 7379); EXPECT_EQ(dfPointCloud.GetNumNormals(), 7379); - } //-------------------------------------------------------------------------