cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script for assertion to check whether the value is 0 or null in groovy script response

SOLVED
Highlighted
Contributor

Groovy Script for assertion to check whether the value is 0 or null in groovy script response

Hi 

 

I have executed the python script from readyAPi successfully, where as in response i get some value, i have to put an assertion, that value should not be 0 or null

 

the response when i run the python script using groovy in readyAPi is.

 

Wed Jun 17 23:48:09 EDT 2020: WARN: 44

 

Please let me know how this can be achieved. 

 

Thanks in Advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

You haven't followed what is suggested.


Regards,
Rao.

View solution in original post

6 REPLIES 6
Highlighted
Community Hero

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

Please show how the python script is run or the script which is being used.


Regards,
Rao.
Highlighted
Contributor

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

@nmrao 

 

Below is the groovy script i am using to execute the python script from ReadyApi

 

def table_count = ["python" , "C:/Pythonscripts/test.py", host,db_password,db_id]
def process = new ProcessBuilder(table_count).redirectErrorStream(true).start()
process.inputStream.eachLine {
log.warn(it)
}
process.waitFor()
return process.exitValue()

 

In the groovy response i got 

 

Thu Jun 18 03:20:19 EDT 2020: WARN: 44

 

So i must put an assertion that 0 or null should not come in the response.  

 

Thanks

Highlighted
Community Hero

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

what is the value of "process.exitValue()"?
Using that you can have the assertion.


Regards,
Rao.
Highlighted
Contributor

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

@nmrao 

 

Thank you

 

Now i have written the script like below, the script is executed fine where as 

 

def table_count = ["python" , "C:/Pythonscripts/test.py", host,db_password,db_id]
def process = new ProcessBuilder(table_count).redirectErrorStream(true).start()
def count = 0
process.inputStream.eachLine {
count=log.warn(it)
assert count !=0 : "The table count is Zero or null"
}
process.waitFor()
return process.exitValue()

 

When i change the script to 

 

def table_count = ["python" , "C:/Pythonscripts/test.py", host,db_password,db_id]
def process = new ProcessBuilder(table_count).redirectErrorStream(true).start()
def count = 0
process.inputStream.eachLine {
count=log.warn(it)
assert count ==0 : "The table count is Zero or null"
}
process.waitFor()
return process.exitValue()

 

i am getting error

 

java.lang.AssertionError: The table count is Zero or null. Expression: (count == 0). Values: count = null error at line: 17

 

In the error message its showing count = null but actual value is 44, So how in the error message the actual count i will get?

 

Thanks

Highlighted
Community Hero

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

You haven't followed what is suggested.


Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: Groovy Script for assertion to check whether the value is 0 or null in groovy script response

Hi @nmrao 

 

Thanks for the update i have kept assertion on log.warn(it), below is the new code i have written and it worked.

 

def table_count = ["python" , "C:/Pythonscripts/test.py", host,db_password,db_id]
def process = new ProcessBuilder(table_count).redirectErrorStream(true).start()
process.inputStream.eachLine {
log.info(it)

assert it !=0 : "The count is Zero"
assert it != null : "The count is null"
}
process.waitFor()
return process.exitValue()

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors