0

I am configuring CMake build project on Windows for MSVC++ project.It build ok the executable,then installs it into a defined directory.In my cases that's:

${CMAKE_SOURCE_DIR}/x64/${CMAKE_BUILD_TYPE}/

The executable has got a folder in the same directory with files which it loads upon the launch.If I launch the .exe manually it opens up and runs ok.But I want to do it via ctest. I defined ctest like this:

add_test(ENGINE_TEST1 ${CMAKE_SOURCE_DIR}/x64/${CMAKE_BUILD_TYPE}/MyApp.exe 
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/x64/${CMAKE_BUILD_TYPE})

When I call from the cmd:

ctest

The executable is starting up but crashes immediately with the error:

Debug Error!

Program:../..../.../MyApp.exe

R6010 -abort() has

been called.

Indeed,when checking the CMake's Last Test.log file it shows that it runs the test not in "WORKING_DIRECTORY but in the directory where the MyApp.exe has been built by CMake.How do I change that?

Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • 1
    Care read [documentation](https://cmake.org/cmake/help/v3.0/command/add_test.html) for `add_test`: for use WORKING_DIRECTORY option you need to use NAME option for name and COMMAND - for command. Short version of `add_test` doesn't accept WORKING_DIRECTORY option. – Tsyvarev Jan 14 '16 at 17:57
  • And the long version of add_test complains also of missing config which is not clear from the documentation how to set.I finally solved it by separately set WORKING_DIRECTLY. – Michael IV Jan 16 '16 at 19:27
  • It would be helpful to others (e.g., me) if you post your solution. – Phil Jan 19 '16 at 23:09

1 Answers1

0

As I am not CMake pro I am sure the following answer is not the optimal way to do it,but at least it works for me. Again,I was trying to run ctest on an executable from within the directory into which cmake had installed it.The exe on the startup was trying to load dependent files which were in the same directory.But it was crashing because it couldn't fin the files. It appears that the cmake default workspace directory is the directory where the cmake files and projects are generated.That's 'build' directory.So when the executable is launched via ctest it search the paths of the files to load relative to the build directory. Now,CMAKE has 2 variations of add_test() method.One is simple with arguments:

 add_test([test_name]  [test exe path])

It doesn't take care of the working directory.

And another one which is explained here does include an argument for explicit setup of the working directory.

Frankly speaking,I wasn't able to get this advanced function working as it was demanding to supply some test .config which I didn't understand how to setup.So what I did,I used the simple add_test function. And then I set the working directory to the location of my executable using this:

 set_tests_properties(mytest PROPERTIES WORKING_DIRECTORY "${TEST_WOKRING_DIR}")

And it fixed the problem.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
  • Thanks for the answer. So far, I have been able to make sure my tests run from the directory to which they are built or installed, so I have not needed to specify a working directory. I will bookmark your answer if/when I need the same capability. As for the configurations option of the add_test command, the docs indicate that it is optional, so you should be able to leave it out. I think that option allows you to use ``CONFIGURATIONS Release`` to restrict the test to only run when using ``ctest -C Release`` to test the release build, which is useful when the DEBUG unit test takes too long. – Phil Jan 21 '16 at 17:24