stefan2904 / shunit2

Automatically exported from code.google.com/p/shunit2
0 stars 0 forks source link

Easy execution of single test instead of entire test suite #23

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
As described in the TODO.TXT:

"Make it possible to execute a single test by passing the name of the test on
the command line"

It would be very nice if it would be possible to execute single tests. Often, 
when i'm writing tests, I have to first test the test to know it is working 
properly, before I trust its output. 

Often, I don't know if something will work, and just running the test is the 
easiest thing to do to find out. However I have to run all tests each time, 
there seems to be no option to execute just a single specific test. 

So this feature would be very much appreciated. 

Original issue reported on code.google.com by Louwrentius on 7 Jun 2010 at 9:58

GoogleCodeExporter commented 9 years ago
Here's a work-around: just define your test suite with 'test*' case names as 
usual, but provide a suite() function like this:

suite ()
{
    for testcase in "${TEST_CASES}" ; do
        suite_addTest $testcase
    done
}

Then you can say:

 $ TEST_CASES="test1 test3" ./my_tests.sh

Maybe not as good as command line arguments, but you could easily grab the 
command line and process it similarly.

Original comment by ric...@gmail.com on 5 Jan 2011 at 3:37

GoogleCodeExporter commented 9 years ago
shunit2 code comments say suite function is deprecated as of 2.1.  But that's 
not in the docs, and there's no obvious replacement.

it's a good workaround, but if you define suite(), then it would *seem* that 
the auto-detection will not happen. I have a vague memory that defining a 
function conditionally is not recommended in shell.  So to get more 
complicated, you could put it in a separate file suite.inc, and have in your 
main test script:  (I forget which is more portable: test -n or test -z)

if [ -z "${TEST_CASES}" ]; then 
    # only define suite() function if TEST_CASES is defined
    . suite.inc
fi

Original comment by danlo...@gmail.com on 7 Jan 2011 at 11:53

GoogleCodeExporter commented 9 years ago
nope... this doesn't work, at least as given.  suite_addTest() is not defined 
until after . ./shunit2,  but suite() (and its contents) must be defined before 
. ./shunit2.

I'm not sure how this should work.

Original comment by danlo...@gmail.com on 8 Jan 2011 at 7:38

GoogleCodeExporter commented 9 years ago
Hm, works for me, so I should probably give more context.

I have a file included via '.' that contains the suite() function shown above. 
It is included early in the specific test file.

Next (lexically) come the test case functions.

Finally, at the very end, I '.'-include shunit2. (version 2.1.5pre)

Why it works:

The introspection of shunit2 is triggered by the internal variable 
'__shunit_suite' being empty. This is the case, even if suite() exists, as long 
as it makes no suite_addTest calls.  The source comment is a bit misleading, 
because it ignores the possibility of a vacuous suite() function.

As for suite_addTest not being defined, that is not a problem, since suite() 
won't actually be called until shunit2 has been interpreted. 

Original comment by ric...@gmail.com on 19 Jan 2011 at 9:47

GoogleCodeExporter commented 9 years ago
What was stated in comment #4 is correct. The introspection shUnit2 does makes 
your calls to suite_addTest() work (inside a suite function).

I chose to deprecate suite() because it is no longer included in jUnit as it 
was when I first wrote shUnit2 years ago. I've never heard of anybody really 
using it, but you're making a point that perhaps I should not deprecate it, but 
rather provide some sample code that demonstrates some potential non-standard 
uses (such as running a single test). I'll think about it.

In the meantime, the things mentioned here seem the best route for now.

Original comment by kate.war...@gtempaccount.com on 15 Mar 2011 at 12:25

GoogleCodeExporter commented 9 years ago

Original comment by kate.war...@gtempaccount.com on 15 Mar 2011 at 12:30

GoogleCodeExporter commented 9 years ago
Can we use a env var like e.g. SHUNIT_TESTS="testAbcd*" to select a subset of 
tests to run. This could be applied in the internal suite.

Original comment by st.k...@gmail.com on 24 Mar 2014 at 10:40