1

I want to disable certain test cases based on the value of some test suite properties (i.e if the property IsActive1 = false, then testcase1 will be disabled).

I used this code at test suite setup script but got an error:

def testSuite = context.testCase.testSuite; 
def totalTestCases = testSuite.getTestCases().size(); 
for(n in (0..totalTestCases-1)) { 
  if(testSuite.getTestCaseAt(n).name == "${#TestSuite#IsActive}"){
     testSuite.getTestCaseAt(n).setDisabled(true) 
  }
}

How can I do that?

albciff
  • 18,112
  • 4
  • 64
  • 89
M.Gawad
  • 65
  • 1
  • 2
  • 9
  • Can you please elaborate your use case please? – Rao Aug 23 '16 at 17:18
  • Do you mean that disable test case(s) based some condition? – Rao Aug 24 '16 at 03:58
  • Yes Rao, i want to add a property in test suite custom properties and call it for example IsActive with value true or false if IsActive value is true then the testcase will be enabled if the value is false then the test case will be disabled, and my test suite contains at least 5 test cases, do you have any clue how to handle that? – M.Gawad Aug 24 '16 at 08:50
  • Do you want any specific list of test cases to enable or disable? you can write a test suite setup script. – Rao Aug 24 '16 at 08:52
  • 1
    I tried to do so but always not working i just want to import test suite custom properties file and as per the value of IsActive i do enable or disable the test case i used this code at test suite setup script but got an error : def testSuite = context.testCase.testSuite; def totalTestCases = testSuite.getTestCases().size(); for(n in (0..totalTestCases-1)) { if(testSuite.getTestCaseAt(n).name == "${#TestSuite#IsActive}") testSuite.getTestCaseAt(n).setDisabled(true) } – M.Gawad Aug 24 '16 at 09:28
  • You can check the answers. – Rao Aug 24 '16 at 10:33
  • [Accept the answer](http://stackoverflow.com/help/someone-answers) most useful to you. – Rao Aug 24 '16 at 12:21
  • Besides the answers below, this might give you some other ideas: https://siking.wordpress.com/2014/01/29/defining-test-categories-in-soapui/ – SiKing Aug 25 '16 at 16:24

2 Answers2

1

You can do that with a Groovy script. You could place this script in the Setup script tab on testSuite Window, in order that the script is executed before your testSuite, disabling/enabling testCases depending on the properties.

Based on your requeriments this script could be something like:

// get property names getting only
// the one which start with "isactive"
def isActiveList = testSuite.getPropertyNames().findAll {
    it.toLowerCase().startsWith('isactive')
}
log.info isActiveList

// get testCaseList in order
def testCaseList = testSuite.getTestCaseList()

// for each "isactive" property
// enable or disable the corresponding
// testCase
isActiveList.each{ name ->
    // get the value
    def isActive = testSuite.getPropertyValue(name).toBoolean()
    // get the order from the property name
    def testCaseIndex = name.toLowerCase() - 'isactive'
    log.info "testCase at index $testCaseIndex will be disabled ${!isActive}"
    // get the testCase and enable/disable depend
    // on isactive property
    // NOTE: IsActive1 corresponds to array pos 0
    // so a -1 is necessary
    log.info testCaseList[testCaseIndex.toInteger()-1]?.disabled = !isActive
}

Maybe it's better to rename your properties based on the testCase name's to enable/disable instead of using a number; to avoid undesired behaviors if you change i.e the testCase order or add a new one...

Hope it helps,

albciff
  • 18,112
  • 4
  • 64
  • 89
1

Here is how I would go to achieve this.

  • Assuming that there is a suite with test cases, namely TestCase1, TestCase2, TestCase3, TestCase4, TestCase5. Using this approach, it would be a simple change of the property value to include or exclude the test case name to be disable. I believe that this would be better to change things from one place(single property) instead of changing IsActive property for each test case. If you have more cases to handle this would be a burden (going to each case and modify true or false for IsActive)
  • Create a custom property at test suite level, say DISABLE_TESTS with value (comma separated list to disable) as TestCase1, TestCase5

Here is the Setup Script at Suite level which would achieve to selectively execute the test cases.

/**
* Test Suite Setup Script
* Enable or disable selected list of test cases
* Based on the test suite property DISABLE_TESTS value
**/
//Disables a Test case
def disableTestCase(testCaze) {
    log.info "Disabling test case : ${testCaze.name}"
    testCaze.disabled = true
}

//Enables a Test case
def enableTestCase(testCaze) {
    log.info "Enabling test case : ${testCaze.name}"
    testCaze.disabled = false
}

//Read the test case names to enable and convert it to list
def disableList = testSuite.getPropertyValue('DISABLE_TESTS')?.split(',')?.collect { it.trim()} ?: []
log.info "List of test for disable: ${disableList}"
//Loop thru the test cases and enable or disable
testSuite.testCaseList.each { kase ->
    //If the above list contains test case name disable, enable otherwise
    if (disableList && disableList.contains(kase.name)) {
        disableTestCase(kase)
    } else {
        enableTestCase(kase)
    }
}

Since some of the cases are disabled, it would be good to enable all the cases as part of Test Suite Teardown Script. It would look mostly same, but there wont be any condition.

/**
* Test Suite Teardown Script
* Which enables all the test cases after selective execution
**/
//Enables a Test case
def enableTestCase(testCaze) {
    log.info "Enabling test case : ${testCaze.name}"
    testCaze.disabled = false
}

//Loop thru the test cases and enable
testSuite.testCaseList.each { kase ->
    enableTestCase(kase)    
}
Rao
  • 20,781
  • 11
  • 57
  • 77
  • @Rao oooouchhh... you win `;)` – albciff Aug 24 '16 at 12:54
  • @albciff, you are awesome, sportive, and helpful always. May be I got points, but you win me. I believe that Marwa can't accept two, otherwise she would have done it. :) – Rao Aug 24 '16 at 12:57
  • @Rao hahaha... you made me blush bro `:)`. Don't worry about the acceptation it's part of the "game" – albciff Aug 24 '16 at 13:02
  • @albciff i really tried to accept both solutions but i can only accept one but really thanks for your support alot both of you , already kept both solutions in my library – M.Gawad Aug 24 '16 at 14:50