SoapUi的“包含”断言中的正则表达式会导致断言总是失败吗?

时间:2019-04-10 21:32:05

标签: soapui

我正在使用SoapUi 5.5.0为我的REST Web服务创建功能测试。我的项目已经可以发出REST请求并显示JSON响应。现在,我需要对此响应做出断言。但是我无法断言我想要的工作方式。

为示例起见,假设我想断言JSON响应包含字符串“ args”或“ brgs”。如果JSON包含这些字符串中的一个或两个,则断言将通过;否则,将断言。否则断言将失败。为此,我打开“ REST请求”测试步骤并添加一个新的“包含”断言。我配置断言以检查JSON响应是否包含与正则表达式“ [ab] rgs”的匹配。

执行REST请求之后,我可以看到JSON响应包含(除其他外)字符串"args": {},。我希望我的正则表达式“包含”声明在这一点上能够通过。但是断言仍然说它失败了。断言总是失败。我无法通过单个正则表达式“包含”断言。我是否从根本上误解了SoapUi的工作方式?还是程序出错了?

作为参考,这是我正在测试的REST端点的JSON响应:

{
   "args": {},
   "headers":    {
      "Accept-Encoding": "gzip,deflate",
      "Host": "httpbin.org",
      "User-Agent": "Apache-HttpClient/4.1.1 (java 1.5)"
   },
   "origin": "199.82.243.104, 199.82.243.104",
   "url": "https://httpbin.org/get"
}

这是我的“包含”声明的配置的图片: assertion configuration

这是我的整个SoapUi项目文件:

<?xml version="1.0" encoding="UTF-8"?>
<con:soapui-project id="ee46fa47-c454-437d-82a3-1b61154f78ea" activeEnvironment="Default" name="REST Project 1" resourceRoot="" soapui-version="5.5.0" abortOnError="false" runType="SEQUENTIAL" xmlns:con="http://eviware.com/soapui/config">
    <con:settings/>
    <con:interface xsi:type="con:RestService" id="a6af7b2e-3c2a-4419-8524-141b5cbe02b5" wadlVersion="http://wadl.dev.java.net/2009/02" name="https://httpbin.org" type="rest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <con:settings/>
        <con:definitionCache type="TEXT" rootPart=""/>
        <con:endpoints>
            <con:endpoint>https://httpbin.org</con:endpoint>
        </con:endpoints>
        <con:resource name="Get" path="/get" id="29a38000-2a32-420a-b33e-f978cc1bd088">
            <con:settings/>
            <con:parameters/>
            <con:method name="Get 1" id="8d40ae42-98f1-4bcb-8247-7fdfad1415d6" method="GET">
                <con:settings/>
                <con:parameters/>
                <con:representation type="RESPONSE">
                    <con:mediaType>application/json</con:mediaType>
                    <con:status>200</con:status>
                    <con:params/>
                    <con:element xmlns:get="https://httpbin.org/get">get:Response</con:element>
                </con:representation>
                <con:request name="Request 1" id="f5c85bda-524b-4d2f-baf7-635f88774a17" mediaType="application/json">
                    <con:settings>
                        <con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;xml-fragment/></con:setting>
                    </con:settings>
                    <con:endpoint>https://httpbin.org</con:endpoint>
                    <con:request/>
                    <con:originalUri>https://httpbin.org/get</con:originalUri>
                    <con:credentials>
                        <con:authType>No Authorization</con:authType>
                    </con:credentials>
                    <con:jmsConfig JMSDeliveryMode="PERSISTENT"/>
                    <con:jmsPropertyConfig/>
                    <con:parameters/>
                </con:request>
            </con:method>
        </con:resource>
    </con:interface>
    <con:testSuite id="6cd7e9db-fb93-4f99-a47c-f0b9a0450e16" name="TestSuite 1">
        <con:settings/>
        <con:runType>SEQUENTIAL</con:runType>
        <con:testCase id="3b79550c-f88b-455e-9c26-008971a68641" failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="TestCase 1" searchProperties="true">
            <con:settings/>
            <con:testStep type="restrequest" name="REST Request" id="2ada1bc8-3541-4918-a937-d83de9b2e395">
                <con:settings/>
                <con:config service="https://httpbin.org" methodName="Get 1" resourcePath="/get" xsi:type="con:RestRequestStep" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <con:restRequest name="REST Request" id="d17f2536-e9a3-4ddb-ad93-08db2e4a5551" mediaType="application/json">
                        <con:settings>
                            <con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;xml-fragment/></con:setting>
                        </con:settings>
                        <con:encoding>UTF-8</con:encoding>
                        <con:endpoint>https://httpbin.org</con:endpoint>
                        <con:request/>
                        <con:originalUri>https://httpbin.org/get</con:originalUri>
                        <con:assertion type="Simple Contains" id="8e6831f2-a067-4dc5-9ba7-163a73dd003b" name="Contains">
                            <con:configuration><token>[ab]rgs</token><ignoreCase>false</ignoreCase><useRegEx>true</useRegEx></con:configuration>
                        </con:assertion>
                        <con:credentials>
                            <con:authType>No Authorization</con:authType>
                        </con:credentials>
                        <con:jmsConfig JMSDeliveryMode="PERSISTENT"/>
                        <con:jmsPropertyConfig/>
                        <con:parameters/>
                    </con:restRequest>
                </con:config>
            </con:testStep>
            <con:properties/>
        </con:testCase>
        <con:properties/>
    </con:testSuite>
    <con:properties/>
    <con:wssContainer/>
    <con:oAuth2ProfileContainer/>
    <con:oAuth1ProfileContainer/>
</con:soapui-project>

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您误解了SoapUI如何处理Contains断言中的正则表达式。

您的正则表达式[ab]rgs期望整个正文仅包含该文本。可能不是您想要的。

如果您希望表情在身体的任何地方,则必须为:.*[ab]rgs.*,其中.*当然代表任何文本。

reference

相关问题