【问题标题】:TeamCity build Succeeds although my Jmeter test fails on Response Assertion尽管我的 Jmeter 测试在响应断言上失败,但 TeamCity 构建成功
【发布时间】:2019-07-09 00:21:00
【问题描述】:

我有一些用 Jmeter 编写的 API 测试,并通过我们的 CI 工具 TeamCity 运行它们。问题是 TeamCity 总是将构建显示为成功,尽管我可以在我的 Jmeter 报告中看到测试中的一些断言失败。我想要的是,在这种情况下,TeamCity 会相应地将构建显示为失败。

TeamCity 配置使用命令行作为触发以下命令的构建步骤:

jmeter -n -t jMeterTeamCityExample.jmx -l log\api_tests_log.jmx -e -o api-tests-results

这是我为测试目的而制作的示例 .jmx 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="3.2" jmeter="3.3 r1808647">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="jMeter + TeamCity example" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</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">stopthread</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>
        <longProp name="ThreadGroup.start_time">1530777613000</longProp>
        <longProp name="ThreadGroup.end_time">1530777613000</longProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="GET post" 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"></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>
          <ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
            <collectionProp name="Asserion.test_strings">
              <stringProp name="51508">400</stringProp>
            </collectionProp>
            <stringProp name="Assertion.test_field">Assertion.response_code</stringProp>
            <boolProp name="Assertion.assume_success">false</boolProp>
            <intProp name="Assertion.test_type">8</intProp>
          </ResponseAssertion>
          <hashTree/>
        </hashTree>
      </hashTree>
    </hashTree>
    <WorkBench guiclass="WorkBenchGui" testclass="WorkBench" testname="WorkBench" enabled="true">
      <boolProp name="WorkBench.save">true</boolProp>
    </WorkBench>
    <hashTree/>
  </hashTree>
</jmeterTestPlan>

在文件中,我有意在响应断言中添加了无效响应代码 (400),导致测试失败。

结论和问题:当我使用上面提供的命令通过 TeamCity 运行此测试时,TeamCity 显示成功,尽管测试本身在响应断言上失败。如果来自 Jmeter 测试计划的任何响应断言失败,我希望 TeamCity 显示失败。 有人知道吗?

注意:我知道一些用于 TeamCity 的 Jmeter 插件,但我想通过仅使用命令行构建步骤来找到解决方案。

【问题讨论】:

标签: api jmeter continuous-integration teamcity web-api-testing


【解决方案1】:

我找到了一个不太优雅的解决方案。

  1. 添加构建步骤以将jtl 文件转储到团队城市构建日志:

我在 Windows 上使用此命令行创建了命令行构建步骤:powershell -nologo "&amp; "Get-Content &lt;location of your .jtl file&gt; -Tail 100"

在 linux 上你会这样做:cat &lt;location of your .jtl file&gt;

  1. 我添加了构建失败条件以在构建日志中查找此文本 “文字错误”

这是构建日志的一部分,它显示了我在构建日志中失败的仪表断言:

[18:00:31]Step 5/5: check file for failed assertions (Command Line)
[18:00:31][Step 5/5] Starting: C:\BuildAgent\temp\agentTmp\custom_script6595741193421826142.cmd
[18:00:31][Step 5/5] in directory: C:\apache-jmeter-5.1.1\JmeterREsults\Assertionfailureexample\106
[18:00:31][Step 5/5] 1562598029549  58  HTTP Request API missioncontrol May2019 200 OK  Thread Group 1-1    text    false   Value expected to match regexp '114.13295998454568', but it did not match: '[112.13295998454568,113.30262810285055,82.08123404643247]'  16425   741 1   1   https://xxxxx-api.yyyyy
.com/api/page_mission_control/  58  0   0
[18:00:31][Step 5/5] Process exited with code 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多