I&#39;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 &quot;Build&quot;, 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">&lt;<a href="mailto:andi@lisas.de">andi@lisas.de</a>&gt;</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>
&gt; Date: Wed, 21 Dec 2011 11:52:27 -0500<br>
&gt; From: David Cole &lt;<a href="mailto:david.cole@kitware.com">david.cole@kitware.com</a>&gt;<br>
&gt; Subject: Re: [CMake] Target to build only unit tests?<br>
&gt; To: Robert Dailey &lt;<a href="mailto:rcdailey@gmail.com">rcdailey@gmail.com</a>&gt;<br>
&gt; Cc: CMake ML &lt;<a href="mailto:cmake@cmake.org">cmake@cmake.org</a>&gt;<br>
&gt; Message-ID:<br>
&gt;       &lt;<a href="mailto:CAAdwe9XG5mOOEnNxWSx0GYLCZwqC_7TuYBY%2B8ZO0W4tu%2B-cHcg@mail.gmail.com">CAAdwe9XG5mOOEnNxWSx0GYLCZwqC_7TuYBY+8ZO0W4tu+-cHcg@mail.gmail.com</a>&gt;<br>
&gt; Content-Type: text/plain; charset=ISO-8859-1<br>
<div class="im">&gt;<br>
&gt; On Wed, Dec 21, 2011 at 11:29 AM, Robert Dailey &lt;<a href="mailto:rcdailey@gmail.com">rcdailey@gmail.com</a>&gt; wrote:<br>
&gt; &gt; Is there a target generated by CMake to build ONLY unit tests? I&#39;m using<br>
&gt; &gt; Visual Studio 2008.<br>
&gt; &gt;<br>
&gt;<br>
&gt; No, there is not.<br>
&gt;<br>
&gt;<br>
&gt; &gt; If not, is there a way I can make one?<br>
&gt;<br>
&gt; Yes, of course. You can do anything you want with a combination of<br>
&gt; 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 &quot;unit_test_&quot;? (...too?)<br>
set(unit_test_targets_common_name_prefix &quot;ut_&quot;)<br>
<br>
function(unit_test_register_conditional _target)<br>
  if(_target MATCHES &quot;^${unit_test_targets_common_name_prefix}&quot;)<br>
    add_dependencies(unit_tests_all ${_target})<br>
  endif(_target MATCHES &quot;^${unit_test_targets_common_name_prefix}&quot;)<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&#39;t fetch<br>
your morning cereals - it should still contain functionality<br>
that&#39;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&#39;s a build shell script, one could implement<br>
poor man&#39;s unit test handling by doing something like:<br>
<br>
[ ... run build stuff ... ]<br>
pseudo_random=$(date +%s) # UNIX Epoch seconds (don&#39;t use %N nanoseconds since it&#39;s not portable, plus it&#39;s overkill)<br>
run_tests=$((${pseudo_random} % 42)) # modulo, obviously<br>
if [ ${run_tests} -eq 0 ]; then<br>
  echo &quot;INFO: running unit tests...&quot;<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>