Skip to content

Unit Tests

Running Tests

When LAGRANGE_UNIT_TESTS is ON, unit tests are built automatically. Running unit tests can be done either

  • Option 1 From the command-line in your build folder:

    ctest # or
    make test # or
    ninja test
    
  • Option 2 From Visual Studio or Xcode, by executing the special target RUN_TESTS:

    • Visual Studio

      unit-tests-msvc
      unit-tests-msvc

    • Xcode

      ![unit-tests-xcode](img/unit-tests-xcode.png ```

Debugging

When running unit tests through CMake/CTest, logs for the last failed tests are available under

<build>/Testing/Temporary/LastTestsFailed.log

You can run tests individually, all test executables are located in the tests folder, e.g.:

./tests/test_lagrange_core

Some tests are tagged [.slow] and skipped by default. If you wish to run them, you need to pass [slow] as an argument, or the name of a specific test, e.g.:

./tests/test_lagrange_core "[slow]"

Running Individual Tests

You can also run only your specific test case(s) by passing the name(s) as argument to the test executable. For more details on the available command-line options, please refer to this page of the Catch2 documentation. )

./tests/test_lagrange_core "MeshCreation"

Writing Unit Tests

Writing new unit tests should be pretty straightforward. Please refer to the Catch2 documentation to get you started. However, there are some idiosyncrasies specifics to Lagrange:

  • Unit test data is stored in a separate data repository. Assets are downloaded by CMake, and can be loaded via the convenience function lagrange::testing::load_mesh():

    TEST_CASE("thicken_and_close_mesh stanford-bunny", "[mesh][thicken_and_close_mesh][stanford-bunny]")
    {
        auto mesh = lagrange::testing::load_mesh<TriangleMesh3Df>("open/core/stanford-bunny.obj");
        REQUIRE(...);
    }
    
  • If a test may be failing, e.g., because it is a work in progress, you may use the LA_MAYFAIL_FLAG tag. Contrary to Catch2's [!mayfail], our LA_MAYFAIL_FLAG tag expands to an empty string when Lagrange is compiled during continuous integration, in order to avoid reporting false positives.

    TEST_CASE("Constrained Quadratic Problem 0", "[decimation]" LA_MAYFAIL_FLAG)
    {
        // ...
    }
    

Writing Benchmarks

Benchmarking code should be written alongside regular unit tests, and be tagged with the special flag [!benchmark]. Please read this page for detailed instructions regarding Catch2's benchmarking macros.

By default, Catch2 runs benchmarks with 100 samples to produce accurate average timings. If your benchmark takes some time to run, you might want to use fewer samples.