如何在soapui的groovy类中使用testrunner.fail

如何在soapui的groovy类中使用testrunner.fail,groovy,soapui,Groovy,Soapui,如果字符串值不相同,我试图使测试用例失败。我创建了一个类和一个方法来比较字符串值 public class test1 { public String onetwo(str1,str2) { def first = str1 def second=str2 if (first==second) { return "Strings are same" } else { testrunner.fail("St

如果字符串值不相同,我试图使测试用例失败。我创建了一个类和一个方法来比较字符串值

public class test1 {
public String onetwo(str1,str2) 
{

    def first = str1
    def second=str2
    if (first==second)
    {
        return "Strings are same"
    }
    else
    {
        testrunner.fail("String Values are not same")
    }
}

public String fail(reason)
{
     return "implementation of method1"+reason
 }
}

def objone =new test1()
def result = objone.onetwo('Soapui','Soapui')
def result1 = objone.onetwo('Soapui','SoapuiPro')
在执行它的时候,我得到了上面代码最后一行的消息 错误:groovy.lang.MissingPropertyException:没有这样的属性:类test1的testrunner

如果字符串不相同,请建议如何使用testrunner.fail或任何其他方法使测试用例失败


谢谢

它找不到SoapUI的testrunner,因为您正在自己的类中访问它。(请注意,错误消息试图查找test1.testrunner,当然它不存在。)如果从脚本的顶层(定义变量的地方)访问testrunner,它应该可以工作

如果您仍然想要一个可重用的类/方法,一个简单的修复方法是让它返回一个布尔值或错误消息,然后如果您的方法返回false/error,则调用testrunner.fail。类似这样(尽管布尔返回可能会使代码更简洁):


另一个站点还描述了一个更为复杂的解决方案,即为SoapUI创建可重用的Groovy库。

如果要在
Groovy脚本
测试步骤中比较两个字符串,则不需要编写类,而是可以通过下面的语句实现

匹配样本-请注意,成功断言时不会发生任何事情

字符串比较,如果不相等则失败
assert'Soapui'=='Soapui',“实际和预期不匹配”

另一个例子-使用boolen
def result=true;断言结果,“结果为假”

不匹配示例-失败时测试失败

字符串不匹配,并显示错误消息
assert'Soapui'='SoapuiPro',“实际和预期不匹配”

非零测试的另一个示例
def number=0;断言数字,“数字为零”

如果您只需要类的示例,并且希望访问
testRunner
对象,那么我们需要将其传递给
或需要testRunner的
方法。否则,其他类不知道groovy脚本可用的对象

以下是关于不同测试用例层次结构级别的对象可用性的更多信息。

当soapUI启动时,它会初始化某些变量,并在项目、套件、测试用例、设置脚本、拆卸脚本等处可用。如果打开脚本编辑器,您将看到其中可用的对象


例如,
groovy脚本
测试步骤具有
日志、上下文、testRunner、testCase
对象。但是,如果有人在Groovy脚本测试步骤中创建了一个类,那么这些对象在该用户定义的类中不可用

testRunner
,大写字母“R”!
public class test1 {
    public String onetwo(str1,str2) 
    {

        def first = str1
        def second=str2
        if (first==second)
        {
            return "Strings are same"
        }
        else
        {
            return "String Values are not same"
        }
    }
...
}

def objone =new test1()
def result = objone.onetwo('Soapui','Soapui')
def result1 = objone.onetwo('Soapui','SoapuiPro')

if (result != "Strings are same")
{
    testrunner.fail(result)
}
...