0

Lets say I have a script that has a bunch of functions that act like test cases. So for example:

#!/bin/bash

...

function testAssertEqualsWithStrings () {
    assertEquals "something" "something"
} 
testAssertEqualsWithStrings  <--- I dont want to do this

function testAssertEqualsWithIntegers () {
    assertEquals 10 10 
} 
testAssertEqualsWithIntegers  <--- I dont want to do this

function testAssertEqualsWithIntegers2 () {
    assertEquals 5 $((10 - 5))
}
testAssertEqualsWithIntegers2  <--- I dont want to do this

function testAssertEqualsWithDoubles () {
    assertEquals 5.5 5.5
}
testAssertEqualsWithDoubles  <--- I dont want to do this

...

Is there a way I can call of these functions in order without having to actually use that explicit function call underneath each test case? The idea is that the user shouldnt have to manage calling the functions. Ideally, the test suite library would do it for them. I just need to know if this is even possible.

Edit: The reason why I dont use just the assert methods is so I can have meaningful output. My current setup allows me to have output such as this:

...
LineNo 14: Passed - testAssertEqualsWithStrings
LineNo 19: Passed - testAssertEqualsWithIntegers
LineNo 24: Passed - testAssertEqualsWithIntegers2
LineNo 29: Passed - testAssertEqualsWithDoubles
LineNo 34: Passed - testAssertEqualsWithDoubles2
...
LineNo 103: testAssertEqualsWithStringsFailure: assertEquals() failed. Expected "something", but got "something else".
LineNo 108: testAssertEqualsWithIntegersFailure: assertEquals() failed. Expected "5", but got "10".
LineNo 115: testAssertEqualsWithArraysFailure: assertEquals() failed. Expected "1,2,3", but got "4,5,6".
LineNo 120: testAssertNotSameWithIntegersFailure: assertNotSame() failed. Expected not "5", but got "5".
LineNo 125: testAssertNotSameWithStringsFailure: assertNotSame() failed. Expected not "same", but got "same".
...

EDIT: Solution that worked for me

function runUnitTests () {
    testNames=$(grep "^function" $0 | awk '{print $2}')
    testNamesArray=($testNames)
    beginUnitTests  #prints some pretty output
    for testCase in "${testNamesArray[@]}"
    do
        :
        eval $testCase
    done
    endUnitTests #prints some more pretty output
}

Then I call runUnitTests at the bottom of my test suite.

R4F6
  • 782
  • 1
  • 9
  • 26
  • @hek2mgl this is not a duplicate of the other. OP does want to execute the functions defined right away, not just list them and execute on demand one of them. And I was having something to answer which was border line and which could have helped the OP and also generate probably comments. Really I think you were too quick. How to reopen a wrongly closed and to quickly closed question? – Jay jargot May 11 '16 at 19:12
  • I placed the close vote in order to focus the work on this in one thread. Actually I think it is an interesting problem. But the key point here is to obtain the list of functions in the script in a reliable way. Executing them is not really a problem then. That's why duplicate. – hek2mgl May 11 '16 at 19:16
  • @hek2mgl Not really a problem... I had imagined that: `eval "$(set | sed -n '/^\(test[a-zA-Z_0-9]*\) () $/ {s/^\(test[a-zA-Z_0-9]*\) () $/\1/;p}')"` But I guess it is bit complex and it cannot be used to execute functions in order of definition. `set` command seems to list functions in its own way... And the OP should agree to go for a naming convention: all functions must start with `test`. – Jay jargot May 11 '16 at 19:25
  • @hek2mgl I see what you meant: it is possible to execute tham in order of definition from the same script were there are defined. This is given almost by the other question and associated answers. OK thx – Jay jargot May 11 '16 at 19:37
  • @R4F6 You should remove all function calls and add in the same script, after the last function definition: `eval "$(sed -n '/^ *function *test[a-zA-Z_0-9]* *() *{ *$/ {s/^ *function *\(test[a-zA-Z_0-9]*\) *() *{ *$/\1/;p}' "$0")"` The functions will be called in the order of definition in the script. In this particular situation, the use of `eval` is safe **when functions have valid names**. – Jay jargot May 11 '16 at 19:50

1 Answers1

0

If you just want to run these functions without calling them, why declare them as functions in the first place?

You either need them to be functions because you are using them multiple times and need the calls to be different each time, in which case I don't see an issue.

Otherwise you just want to run each function once without calling them, which is just running the command. Remove all the function declarations and just call each line.

For this example

#!/bin/bash

...

assertEquals "something" "something" #testAssertEqualsWithStrings
assertEquals 10 10 #testAssertEqualsWithIntegers
assertEquals 5 $((10 - 5)) #testAssertEqualsWithIntegers2
assertEquals 5.5 5.5 #testAssertEqualsWithDoubles

...
Ryukerg
  • 21
  • 6
  • Because I want to be able to have meaningful output of which testcase is which. By declaring the test cases as functions, I assign them a name, and I can get their line number. I use both of these for output. – R4F6 May 11 '16 at 18:46