【问题标题】:Using jMeter for functional e2e testing使用 jMeter 进行功能性 e2e 测试
【发布时间】:2018-04-10 08:18:36
【问题描述】:

我们正在使用 JMeter 对 GraphQL API 进行 e2e 测试,并且存在一些问题,即只有 GUI 向我们显示测试结果,从控制台运行时看不到有用的“测试摘要”,并且 jMeter 总是以“0”退出代码表示我们的 CI“成功”。

我已经复制了下面包含的一个最小示例来表明我正在尝试做的事情:

  • 使用 HTTP 采样器向 JSON API 发出请求
  • 对结果进行“JSON 断言”
  • 使用 bean shell 侦听器将失败的采样器或断言的结果打印到文件或控制台

我的目标是将“查看结果树”之类的内容作为通过/失败摘要打印到运行 jMeter 的控制台,或将其写入文件。

我们的测试使用带有 JSON 断言的简单 HTTP 采样器,我的目标是尝试使用 Bean Shell 侦听器并检查任何采样器或 JSON 断言是否有错误,并在任何一种情况下写入文件。

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="4.0" jmeter="4.0 r1823414">
<hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
    <stringProp name="TestPlan.comments"></stringProp>
    <boolProp name="TestPlan.functional_mode">false</boolProp>
    <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
    <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
    <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
    </elementProp>
    <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
    <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
        <boolProp name="LoopController.continue_forever">false</boolProp>
        <stringProp name="LoopController.loops">1</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
    </ThreadGroup>
    <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
        <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
            <collectionProp name="Arguments.arguments"/>
        </elementProp>
        <stringProp name="HTTPSampler.domain">jsonplaceholder.typicode.com</stringProp>
        <stringProp name="HTTPSampler.port"></stringProp>
        <stringProp name="HTTPSampler.protocol">https</stringProp>
        <stringProp name="HTTPSampler.contentEncoding"></stringProp>
        <stringProp name="HTTPSampler.path">/posts/1</stringProp>
        <stringProp name="HTTPSampler.method">GET</stringProp>
        <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
        <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
        <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
        <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
        <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
        <stringProp name="HTTPSampler.connect_timeout"></stringProp>
        <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <JSONPathAssertion guiclass="JSONPathAssertionGui" testclass="JSONPathAssertion" testname="JSON Assertion" enabled="true">
        <stringProp name="JSON_PATH">$.userID</stringProp>
        <stringProp name="EXPECTED_VALUE"></stringProp>
        <boolProp name="JSONVALIDATION">false</boolProp>
        <boolProp name="EXPECT_NULL">false</boolProp>
        <boolProp name="INVERT">false</boolProp>
        <boolProp name="ISREGEX">true</boolProp>
        </JSONPathAssertion>
        <hashTree/>
    </hashTree>
    <BeanShellListener guiclass="TestBeanGUI" testclass="BeanShellListener" testname="BeanShell Listener" enabled="true">
        <boolProp name="resetInterpreter">false</boolProp>
        <stringProp name="parameters"></stringProp>
        <stringProp name="filename"></stringProp>
        <stringProp name="script"></stringProp>
    </BeanShellListener>
    <hashTree/>
    </hashTree>
</hashTree>
</jmeterTestPlan>

我尝试了具有以下内容的 Bean Shell 侦听器,但在 Bean Shell 侦听器可用的变量/上下文中找不到 JSON 断言结果。

import org.apache.jmeter.services.FileServer;

// Open File(s)
f = new FileOutputStream(FileServer.getFileServer().getBaseDir()+"/results.txt", true); 
p = new PrintStream(f); 

// Write data to file 
p.println( "sampleResult " + sampleResult.toString() + ": " + sampleResult.isSuccessful() );
p.println( "sampleEvent  " + sampleEvent.toString());
p.println( "prev  " + prev.toString());
p.println( "props  " + props.toString());
p.println( "ctx  " + ctx.toString());
p.println( "vars  " + vars.toString());

// Close File(s)
p.close();f.close();

我们不是 Java 商店,我们将 GitLab/etc 用于我们的 CI,理想的情况是能够在控制台中运行我们的 jMeter 测试,并查看输出摘要,就像在执行单元时看到的那样测试脚本语言或类似语言,并带有通过和失败测试的红色/绿色摘要。

【问题讨论】:

    标签: jmeter functional-testing e2e-testing beanshell


    【解决方案1】:
    1. 请注意,根据JMeter 3.1 it is recommended to use JSR223 Test Elements for any form of scripting。所以我建议改用JSR223 Listener
    2. 您可以使用prev.getAssertionResults() 访问AssertionResults
    3. Groovy SDK 允许更轻松地处理文件,因此您可以使用以下内容:

      def results = new File('results.txt')
      def newLine = System.getProperty('line.separator')
      results << 'Sampler Name: ' + prev.getSampleLabel() + ' Successful: ' + prev.isSuccessful() 
      if (!prev.isSuccessful()) {
          prev.getAssertionResults().each {assertionResult ->
              results << ' Error message: ' << assertionResult.failureMessage << newLine
          }
      }
      

      为了得到以下形式的输出:

      Sampler Name: HTTP Request Successful: false Error message: No results for path: $['userID']
      

      有关 JMeter 中的 Groovy 脚本的更多信息,请参阅 Apache Groovy - Why and How You Should Use It 文章。

    您还可以考虑使用Taurus 工具运行测试,该工具提供实时控制台报告和强大的Pass/Fail Criteria subsystem,您可以在其中指定阈值和在达到/超过时要采取的操作。因此,您可以将 Taurus 配置为在出现故障时使用非零代码退出,这可用于脚本或持续集成。

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多