I've managed to workaround this in Visual Studio by using a solution folder for my unit tests. I can then right-click the solution folder and click "Build", and that will build all my tests.<div><br></div><div>
I actually like the dependency idea, it is simple and straight forward. I have common code that defines my tests, so I can easily accumulate a list of those tests and at the end, define a custom target that depends on all of them.</div>
<div><br></div><div>Would be nice to see CMake have builtin support for this, though. There could be a BUILD_TESTS CMake predefined target in visual studio.</div><div><div><br></div><div>---------</div>Robert Dailey<br>
<br><br><div class="gmail_quote">On Wed, Dec 21, 2011 at 1:12 PM, Andreas Mohr <span dir="ltr"><<a href="mailto:andi@lisas.de">andi@lisas.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
On Wed, Dec 21, 2011 at 11:59:13AM -0500, <a href="mailto:cmake-request@cmake.org">cmake-request@cmake.org</a> wrote:<br>
> Date: Wed, 21 Dec 2011 11:52:27 -0500<br>
> From: David Cole <<a href="mailto:david.cole@kitware.com">david.cole@kitware.com</a>><br>
> Subject: Re: [CMake] Target to build only unit tests?<br>
> To: Robert Dailey <<a href="mailto:rcdailey@gmail.com">rcdailey@gmail.com</a>><br>
> Cc: CMake ML <<a href="mailto:cmake@cmake.org">cmake@cmake.org</a>><br>
> Message-ID:<br>
> <<a href="mailto:CAAdwe9XG5mOOEnNxWSx0GYLCZwqC_7TuYBY%2B8ZO0W4tu%2B-cHcg@mail.gmail.com">CAAdwe9XG5mOOEnNxWSx0GYLCZwqC_7TuYBY+8ZO0W4tu+-cHcg@mail.gmail.com</a>><br>
> Content-Type: text/plain; charset=ISO-8859-1<br>
<div class="im">><br>
> On Wed, Dec 21, 2011 at 11:29 AM, Robert Dailey <<a href="mailto:rcdailey@gmail.com">rcdailey@gmail.com</a>> wrote:<br>
> > Is there a target generated by CMake to build ONLY unit tests? I'm using<br>
> > Visual Studio 2008.<br>
> ><br>
><br>
> No, there is not.<br>
><br>
><br>
> > If not, is there a way I can make one?<br>
><br>
> Yes, of course. You can do anything you want with a combination of<br>
> add_custom_command and add_custom_target.<br>
<br>
<br>
</div>Something worthwhile might be (whipped up quickly, untested):<br>
<br>
# Implement include guard (e.g. reduce cmake --trace clutter)<br>
if(UNIT_TEST_HELPERS_DEFINED)<br>
return()<br>
endif(UNIT_TEST_HELPERS_DEFINED)<br>
set(UNIT_TEST_HELPERS_DEFINED true)<br>
<br>
if(NOT TARGET unit_tests_all)<br>
add_custom_target(unit_tests_all ALL)<br>
endif(NOT TARGET unit_tests_all)<br>
<br>
# Properly very worthwhile to have a fixed convention for unit test<br>
# targets. Or perhaps use "unit_test_"? (...too?)<br>
set(unit_test_targets_common_name_prefix "ut_")<br>
<br>
function(unit_test_register_conditional _target)<br>
if(_target MATCHES "^${unit_test_targets_common_name_prefix}")<br>
add_dependencies(unit_tests_all ${_target})<br>
endif(_target MATCHES "^${unit_test_targets_common_name_prefix}")<br>
function(unit_test_register_conditional _target)<br>
<br>
function(super_power_target_post_handling _target)<br>
unit_test_register_conditional(${_target})<br>
...something_else_that_might_be_useful...()<br>
endfunction(super_power_target_post_handling _target)<br>
<br>
<br>
Then push that code into a CMake Module file (to be added to<br>
cmake/Modules/ in your source root or some such),<br>
and do<br>
include(ModuleName)<br>
where needed, and call<br>
super_power_target_post_handling(current_target)<br>
for every target where common post-handling might be useful.<br>
<br>
But obviously super_power_target_post_handling() shouldn't fetch<br>
your morning cereals - it should still contain functionality<br>
that's specific enough, to not end up with messy imprecise (unknown!) handling<br>
that might change at any time.<br>
<br>
<br>
<br>
And then, in case there's a build shell script, one could implement<br>
poor man's unit test handling by doing something like:<br>
<br>
[ ... run build stuff ... ]<br>
pseudo_random=$(date +%s) # UNIX Epoch seconds (don't use %N nanoseconds since it's not portable, plus it's overkill)<br>
run_tests=$((${pseudo_random} % 42)) # modulo, obviously<br>
if [ ${run_tests} -eq 0 ]; then<br>
echo "INFO: running unit tests..."<br>
[[[ result of grep CMAKE_BUILD_COMMAND CMakeCache.txt ]]] unit_tests_all<br>
fi<br>
<span class="HOEnZb"><font color="#888888"><br>
Andreas Mohr<br>
</font></span></blockquote></div><br></div>