Search code examples
xmlgitlabyamlxml-parsinggitlab-ci

how to stop a pipeline if test cases fail in gitlab?


I have this yml file

test1:
  stage: test
  #
  rules:
    - when: 
      if: $TEST_ENABLE == "ON"
  script:
    - ........ logic to run tests ............
    - C:\tpt.exe --tpt_output="xml:logs.xml"
  artifacts:
    paths:
      - targets\test\reports\*.html

I have this stage test1 that runs several test cases and once that is finished i create a logs.xml file that stores all the data like duration, result, testcase number, etc. Currently when the pipeline is triggered the test cases fails but the test stage passes. I think there is no link between pipeline and the xml.

In the logs.xml i can see that result=ERROR. Now I want to parse this inside the pipeline and tell pipeline if result=ERROR then fail the stage. How can I do that? can someone guide me here? Thanks in advance.

What I have tried but does not work:

script:
    - ........ logic to run tests ............
    - C:\tpt.exe --tpt_output="xml:logs.xml"
    - test -f logs.xml && grep -L "ERROR" logs.xml

Solution

  • found the solution:

    script:
      - ........ logic to run tests ............
      - C:\tpt.exe --tpt_output="xml:logs.xml"
      - if (Select-String -Path logs.xml -Pattern "result=ERROR") {  
      xit 1 }