在gitlab上运行测试后,Junit报告未更新

问题描述

我在gitlab cigitlab runner上进行了自动化测试,除报告外,其他所有方法都运行良好。测试junit的报告未更新后,总是显示相同的通过,即使认为cmd显示不同数量的通过测试也不会通过测试。

Gitlab脚本:

    stages:
      - build
      - test
    
    docker-build-master:
      image: docker:latest
      stage: build
      services:
        - docker:dind
      before_script:
        - docker login -u "xxx" -p "yyy" docker.io
      script:
        - docker build ./AutomaticTests --pull -t "dockerImage" 
        - docker image tag dockerImage xxx/dockerImage:0.0.1
        - docker push "xxx/dockerImage:0.0.1"
    
    test:
      image: docker:latest
      services:
        - docker:dind
      stage: test
      before_script:
        - docker login -u "xxx" -p "yyy" docker.io
      script: 
        - docker run "xxx/dockerImage:0.0.1"
      artifacts:
        when: always
        paths:
          - AutomaticTests/bin/Release/artifacts/test-result.xml
        reports:
          junit:
            - AutomaticTests/bin/Release/artifacts/test-result.xml

Dockerfile:

    FROM mcr.microsoft.com/dotnet/core/sdk:2.1
    
    copY /publish  /AutomaticTests
    workdir /AutomaticTests
    RUN apt-get update -y
    RUN apt install unzip
    RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
    RUN curl https://chromedriver.storage.googleapis.com/84.0.4147.30/chromedriver_linux64.zip -o /usr/local/bin/chromedriver
    RUN unzip -o /usr/local/bin/chromedriver -d /AutomaticTests
    RUN chmod 777 /AutomaticTests
    
    CMD dotnet vstest /Parallel AutomaticTests.dll --TestAdapterPath:. --logger:"nunit;LogFilePath=..\artifacts\test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"

解决方法

在我的 gitlab 管道中使用 docker-in-docker 时,我遇到了类似的问题。您在容器内运行测试。因此,测试结果存储在您的“被测容器”中。但是,gitlab-ci 路径引用的不是“被测容器”,而是 docker-in-docker 环境的外部容器。

您可以尝试通过以下方式将测试结果从图像直接复制到您的外部容器:

mkdir reports
docker cp $(docker create --rm DOCKER_IMAGE):/ABSOLUTE/FILEPATH/IN/DOCKER/CONTAINER reports/.

所以,在你的情况下,这将是这样的(未经测试......!):

...

    test:
      image: docker:latest
      services:
        - docker:dind
      stage: test
      before_script:
        - docker login -u "xxx" -p "yyy" docker.io
      script: 
        - mkdir reports
        - docker cp $(docker create --rm xxx/dockerImage:0.0.1):/AutomaticTests/bin/Release/artifacts/test-result.xml reports/.
      artifacts:
        when: always
        reports:
          junit:
            - reports/test-result.xml
...

此外,有关 docker cp 命令的进一步说明,请参阅此帖子:https://stackoverflow.com/a/59055906/6603778

请记住,docker cp 需要您要从容器复制的文件的绝对路径。