I'm adding tests to my project and all the tests have some prerequisites so I add a precheck test which all other tests depend on. If the precheck fails then I'd like the other tests to stop immediately.
add_test(
NAME precheck
COMMAND false
)
add_test(
NAME test-1
COMMAND true
)
add_test(
NAME test-2
COMMAND true
)
set_tests_properties(
test-1 test-2
PROPERTIES
DEPENDS precheck
)
But seems like the DEPENDS property only impact the order of tests:
$ make test
Running tests...
Test project /root/ibvq/frkcrpg/b
Start 1: precheck
1/3 Test #1: precheck .........................***Failed 0.00 sec
Start 2: test-1
2/3 Test #2: test-1 ........................... Passed 0.00 sec
Start 3: test-2
3/3 Test #3: test-2 ........................... Passed 0.00 sec
67% tests passed, 1 tests failed out of 3
Total Test time (real) = 0.02 sec
The following tests FAILED:
1 - precheck (Failed)
Errors while running CTest
Makefile:83: recipe for target 'test' failed
make: *** [test] Error 8
So how can I make the failed precheck stop other tests?
If you are using CMake version 3.7 or later, you can use the test fixture related properties.
For earlier versions of CMake, have your precheck test create a dummy file on success that your other tests depend on by setting the REQUIRED_FILES property.
Related
I am trying to run some tests using CTest.
My CTestTestFile.cmake contains a single test
add_test(NAME test1
COMMAND python script.py args
)
but when I try run ctest it outputs the following
Start 1: NAME
Could not find executable test1
Looked in the following places:
test1
test1.exe
Release/test1
Release/test1.exe
Debug/test1
Debug/test1.exe
MinSizeRel/test1
MinSizeRel/test1.exe
RelWithDebInfo/test1
RelWithDebInfo/test1.exe
Deployment/test1
Deployment/test1.exe
Development/test1
Development/test1.exe
Unable to find executable: test1
1/1 Test #1: NAME .............................***Not Run 0.00 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.14 sec
The following tests FAILED:
1 - NAME (Not Run)
This suggests CTest thinks I'm using the short version of the command, so that my test's name is NAME, the command is test1 and python script.py args are the arguments to the command, when this is of course not the case, as I'm obviously using the long version of the command.
I have CMake version 3.24.1 installed.
The CTest runtime, unlike CMake, accepts only the "basic" signature documented at the end of the usual add_test documentation:
add_test(<name> <command> [<arg>...])
Notice that CTest thought your test was named NAME.
So you have to write:
add_test(test1 "python" "script.py" "args")
When running my unit test (gtest) through Bazel, I'm seeing a failure reported. However, the logs indicate that my test is running successfully and passing.
Other tests in my project are passing and the only difference between the deviant test and the others is that the deviant test is multithreaded.
I've run the test binary found in bazel-bin by itself and it passes and returns successfully.
Bazel version:
Build label: 0.26.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue May 28 08:35:14 2019 (1559032514)
Build timestamp: 1559032514
Build timestamp as int: 1559032514
The relevant block in my BUILD file:
cc_test(
name = "DBControllerIntegration",
srcs = ["dbcontroller_integration_test.cc"],
deps = [
"//src:db_ctl_lib",
"//test/mocks:sstable_mock_lib",
"#boost//:filesystem",
"#com_google_protobuf//:protobuf",
"#glog//:glog",
"#googletest//:gtest_main",
],
copts = ["-std=c++17"],
)
Bazel test failure output:
>> bazel test //test:DBControllerIntegration --test_output=errors
INFO: Invocation ID: ccca8fa7-27a5-4c8c-badf-3f342934e4e5
INFO: Analysed target //test:DBControllerIntegration (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
FAIL: //test:DBControllerIntegration (see /home/tallen/.cache/bazel/_bazel_tallen/f087948e065d612174d90a43a5740198/execroot/diodb/bazel-out/k8-dbg/testlogs/test/DBControllerIntegration/test.log)
INFO: From Testing //test:DBControllerIntegration:
==================== Test output for //test:DBControllerIntegration:
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from DBControllerIntegrationTest
[ RUN ] DBControllerIntegrationTest.Basic
... <omitting my application's logs> ...
[ OK ] DBControllerIntegrationTest.Basic (4000 ms)
[----------] 1 test from DBControllerIntegrationTest (4000 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (4001 ms total)
[ PASSED ] 1 test.
================================================================================
Target //test:DBControllerIntegration up-to-date:
bazel-bin/test/DBControllerIntegration
INFO: Elapsed time: 4.706s, Critical Path: 4.57s
INFO: 1 process: 1 processwrapper-sandbox.
INFO: Build completed, 1 test FAILED, 2 total actions
//test:DBControllerIntegration FAILED in 4.6s
/home/tallen/.cache/bazel/_bazel_tallen/f087948e065d612174d90a43a5740198/execroot/diodb/bazel-out/k8-dbg/testlogs/test/DBControllerIntegration/test.log
INFO: Build completed, 1 test FAILED, 2 total actions
Running the test binary by itself:
>> ./bazel-bin/test/DBControllerIntegration
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from DBControllerIntegrationTest
[ RUN ] DBControllerIntegrationTest.Basic
... <omitting my application's logs> ...
[ OK ] DBControllerIntegrationTest.Basic (4001 ms)
[----------] 1 test from DBControllerIntegrationTest (4001 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (4001 ms total)
[ PASSED ] 1 test.
I would expect the Bazel test result to be reported as passing since the actual test is passing, but I'm seeing the test as failed.
This was resolved when gracefully terminating all threads spawned by my project.
Here is my code in .travis.yml file,after run build is passing, but I found the TEST are failed. I have no idea about this and block me a few days, appreciate your help. THX
--- Error as below ---
2018-12-20 05:57:42.698 xcodebuild[2394:8186] [MT]
IDETestOperationsObserverDebug: (CB8F382A-5B77-4BDA-BC4F-6EDF7B7DB822)
Beginning test session aaaaTests-CB8F382A-5B77-4BDA-BC4F-6EDF7B7DB822 at 2018-
12-20 05:57:42.698 with Xcode 10A255 on target <DVTiPhoneSimulator:
0x7f86387e49d0> {
SimDevice: iPhone 6 (B25EC6DB-0B2F-4920-B6C2-8560331BA779, iOS 9.1, Booted)
} (9.1 (13B143))
2018-12-20 05:57:53.744 xcodebuild[2394:8186] [MT]
IDETestOperationsObserverDebug: 11.068 elapsed -- Testing started completed.
2018-12-20 05:57:53.744 xcodebuild[2394:8186] [MT]
IDETestOperationsObserverDebug: 0.000 sec, +0.000 sec -- start
2018-12-20 05:57:53.744 xcodebuild[2394:8186] [MT]
IDETestOperationsObserverDebug: 11.068 sec, +11.068 sec -- end
2018-12-20 05:57:53.746 xcodebuild[2394:8186] Error
Domain=IDETestOperationsObserverErrorDomain Code=6 "Early unexpected exit,
operation never finished bootstrapping - no restart will be attempted"
UserInfo={NSLocalizedDescription=Early unexpected exit, operation never
finished bootstrapping - no restart will be attempted,
NSUnderlyingError=0x7f86389df930 {Error
Domain=IDETestOperationsObserverErrorDomain Code=5 "Test runner exited before
starting test execution." UserInfo={NSLocalizedDescription=Test runner exited
before starting test execution., NSLocalizedRecoverySuggestion=If you believe
this error represents a bug, please attach the result bundle at
/Users/travis/Library/Developer/Xcode/DerivedData/aaaa-
bdscsxnorhzvygafnsqdiqoriugx/Logs/Test/Run-aaaa-2018.12.20_05-57-22-
+0000.xcresult}}}
Testing failed:
aaaa.app (2488) encountered an error (Early unexpected exit, operation never finished bootstrapping - no restart will be attempted. Underlying error: Test runner exited before starting test execution.)
** TEST FAILED **
The command "set -o pipefail && xcodebuild -workspace aaaa.xcworkspace -scheme aaaa -destination platform\=iOS\ Simulator,OS\=9.1,name\=iPhone\ 6 build test | xcpretty" exited with 65.
Done. Your build exited with 1.
---My code as below---
language: objective-c
osx_image: xcode10
xcode_workspace: aaaa.xcworkspace
xcode_scheme: aaaa
xcode_destination: platform=iOS Simulator,OS=9.1,name=iPhone 6
before_install:
- pod repo update
- npm install ios-sim -g
- ios-sim start --devicetypeid "com.apple.CoreSimulator.SimDeviceType.iPhone-6, 9.1"
The issue can fix by below step:
change the version of osx_image in .travis.yml file and retry
// if xcode10.1 build failed, you can change to other version, like xcode9.4
osx_image: xcode10.1
Travis support xcode version list:
https://docs.travis-ci.com/user/languages/objective-c/
I'm building fortran project with cmake and I can't find solution to print to console FRUIT test results, they look something like these:
Test module initialized
. : successful assert, F : failed assert
7.00000000000000 -3.60000000000000 7.00000000000000
FFF
Start of FRUIT summary:
Some tests failed!
-- Failed assertion messages:
[_not_set_]:Expected [7.00000000000000], Got [1.00000000000000]
[_not_set_]:Expected [-3.60000000000000], Got [2.00000000000000]
[_not_set_]:Expected [7.00000000000000], Got [6.00000000000000]
-- end of failed assertion messages.
Total asserts : 3
Successful : 0
Failed : 3
Successful rate: 0.00%
Successful asserts / total asserts : [ 0 / 3 ]
Successful cases / total cases : [ 0 / 0 ]
-- end of FRUIT summary
The output I'm getting with make test looks like:
make test
Running tests...
Test project /home/konrad/Desktop/fortran
Start 1: unit_tests
1/1 Test #1: unit_tests ....................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.01 sec
And since passing cmake tests doesn't mean passing FRUIT ones, I want to print FRUIT file everytime I run tests (just for the sake of making it work). I've tried adding printing commands at the end of test command (like this less), adding
-P ${CMAKE_TEST_DIR}/unit_tests.txt
at the end of add_test, building custom after build commands (which I can't make to run after make test so if you knew how to do that would solve it as well, seems like make test or test is not really a target)
Last part of my cmake file with all the testing code:
add_executable(task ${TASK_SOURCES})
add_executable(tests ${TEST_SOURCES})
enable_testing()
set(run_command "${CMAKE_BINARY_DIR}/tests")
set(UNIT_TEST_NAME "unit_tests.txt")
file(MAKE_DIRECTORY ${CMAKE_TEST_DIR})
add_test( NAME unit_tests
COMMAND sh -c
"rm -f ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& ${run_command} \
>> ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& less ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME}"
)
I have solved a lack of tests output with custom CMake target that will invoke ctest in verbose mode etc.
e.g.
enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process
--verbose
--output-on-failure
)
The output of cmake tests is captured to a file (Testing/Temporary/LastTest.log in my current project).
cmake tests rely on the return code of the test program, with one test program per test.
If you wish to run a program that is a "driver" for your tests, I recommend to use add_custom_target. This commands will add a target that runs a command of your choice.
For instance:
add_custom_target(Name unit_tests tests)
add_dependencies(unit_tests tests)
I am not sure whether the add_dependencies line is needed in this case though (as tests is a target managed by cmake).
Then, you can run
make unit_tests
and it will run your test driver.
I recently started learning cmake, and have run into a small issue. I got both my executable and the unit tests to compile from the generated makefile without issue. If I run ./test in the build directory, the tests created in UnitTest++ run and complete as expected, printing the results. Is there any way to get make test to simply run the test executable rather than running it inside ctest framework or should I go about this a different way?
Here is a minimal working example of my code:
src/main/main.c is a simple empty main function
src/test/testMain.cpp:
#include <UnitTest++/UnitTest++.h>
TEST(FailSpect)
{
CHECK(false);
}
int main()
{
UnitTest::RunAllTests();
}
CMakeLists.txt:
cmake_minimum_required( VERSION 2.6 )
project( myProject)
enable_testing()
set( myProjectMain
src/main/main.c
)
set( myProjectSrc
)
set( myProjectTestSrc
src/test/testMain.cpp
)
add_executable( myExecutable ${myProjectMain} ${myProjectSrc} )
add_executable( testSuite ${myProjectTestSrc} ${myProjectSrc} )
target_link_libraries( testSuite UnitTest++ )
add_test( testExe testSuite )
make test output:
Running tests...
Start processing tests
Test project /myProjectDir/build
1/ 1 Testing testExe Passed
100% tests passed, 0 tests failed out of 1
./testSuite output:
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
I have sorted out how to do this. First remove the lines:
enable_testing()
and
add_test(testExe testSuite)
and replace them by the line:
add_custom_target(test ./testExe
DEPENDS ./testExe)
at the end of the CMakeLists.txt file. Now make (all) builds both the tests and the main program. If everything is built already, then make test will just check that the tests are built and run them, producing:
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test
If the tests are out of date (after a make clean for instance), then make test will produce:
[100%] Building CXX object CMakeFiles/testExe.dir/src/test/testMain.cpp.o
Linking CXX executable testExe
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test