DFTesting#
Ideally, if we add code to the project, we should also add tests (at least unit tests).
In df we use CTest as a test framework managed by Cmake in the file cmake/tests.cmake
to run:
Tests are in the tests
folder, and here’s its structure:
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:
cmake -S . -B build -A x64 -DBUILD_PYTHON_MODULE=ON -DBUILD_TESTS=ON -DRUN_TESTS=ON
cmake --build build --config Release
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:
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:
@pytest.fixture
def create_DFPointCloudSampleRoof():
df_pcd = dfb.dfb_geometry.DFPointCloud()
df_pcd.load_from_PLY(get_ply_cloud_roof_quarter_path())
yield df_pcd
Than you can use it in your test:
def test_DFPointCloud_apply_color(create_DFPointCloudSampleRoof):
pc = create_DFPointCloudSampleRoof
pc.apply_color(255, 0, 0)
for color in pc.colors:
assert (color[0] == 1 and color[1] == 0 and color[2] == 0), "All colors should be (255, 0, 0)"
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
:
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:
#include <gtest/gtest.h>
#include "diffCheck.hh"
#include "diffCheck/IOManager.hh"
//-------------------------------------------------------------------------
// fixtures
//-------------------------------------------------------------------------
class DFPointCloudTestFixture : public ::testing::Test {
protected:
std::vector<Eigen::Vector3d> points;
std::vector<Eigen::Vector3d> colors;
std::vector<Eigen::Vector3d> normals;
diffCheck::geometry::DFPointCloud dfPointCloud;
DFPointCloudTestFixture() : dfPointCloud(points, colors, normals) {}
void SetUp() override {
dfPointCloud = diffCheck::geometry::DFPointCloud();
std::string pathTest = diffCheck::io::GetRoofQuarterPlyPath();
dfPointCloud.LoadFromPLY(diffCheck::io::GetRoofQuarterPlyPath());
}
void TearDown() override {
// Clean up any resources if needed
}
};
and you can use it in your test:
TEST_F(DFPointCloudTestFixture, HasColors) {
EXPECT_TRUE(dfPointCloud.HasColors());
}