如何在gitlab-ci上获得单元测试的测试结果?

时间:2019-07-08 05:25:35

标签: gitlab-ci python-unittest python-3.7 gitlab-ci-runner

使用gitlab-runner执行python unittest后,测试用例失败,但是gitlab显示通过。

$ python3 test/test_utils.py
test_init (__main__.Test_BslReset) ... ERROR
test_reset_all (__main__.Test_BslReset) ... ERROR

======================================================================
ERROR: test_init (__main__.Test_BslReset)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/test_utils.py", line 9, in setUp
    self.bsl_reset = common.utils.BslReset()
  File "/home/gitlab-runner/builds/FgNVsWZq/0/root/hddl-r-sanity/common/utils.py", line 119, in __init__
    self.bsl_reset_src = os.path.join(get_hddl_install_dir(), 'hddl-bsl')
  File "/home/gitlab-runner/builds/FgNVsWZq/0/root/hddl-r-sanity/common/utils.py", line 65, in get_hddl_install_dir
    raise ValueError('Not check EnvVar(HDDL_INSTALL_DIR),Please set it!')
ValueError: Not check EnvVar(HDDL_INSTALL_DIR),Please set it!

======================================================================
ERROR: test_reset_all (__main__.Test_BslReset)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/test_utils.py", line 9, in setUp
    self.bsl_reset = common.utils.BslReset()
  File "/home/gitlab-runner/builds/FgNVsWZq/0/root/hddl-r-sanity/common/utils.py", line 119, in __init__
    self.bsl_reset_src = os.path.join(get_hddl_install_dir(), 'hddl-bsl')
  File "/home/gitlab-runner/builds/FgNVsWZq/0/root/hddl-r-sanity/common/utils.py", line 65, in get_hddl_install_dir
    raise ValueError('Not check EnvVar(HDDL_INSTALL_DIR),Please set it!')
ValueError: Not check EnvVar(HDDL_INSTALL_DIR),Please set it!

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=2)
Job succeeded

我想在gitlab上使用gitlab-ci来执行python unittest,.gitlab-ci.yml配置文件如下:

stages:
 - build
 - test

build:
 stage: build
 script:
  - sudo apt-get install python3-pip
  - pip3 install -r requirements_linux.txt
 tags:
  - 'server'
test:
 stage: test
 script:
  - python3 test/test_utils.py
 tags:
  - "server"

我认为gitlab只是判断python3 test / test-utils.py这个命令不成功,并且在unittest中并没有真正得到结果,该怎么做?

2 个答案:

答案 0 :(得分:0)

如果您使用 TextTestRunner 作为模块运行测试,它不会以代码 1 退出。

您想捕获结果并像这样调用 wasSuccessful() :

import unittest
from test_app import SomeTestCase

if __name__ == "__main__":
   suite = unittest.TestSuite()
   suite.addTest(SomeTestCase('test_add'))
   result = unittest.TextTestRunner(verbosity=2).run(suite)

   if result.wasSuccessful():
       exit(0)
   else:
       exit(1)

来源:https://code-maven.com/python-unittest-fails-exit-code-0

答案 1 :(得分:0)

可以在 GitLab 的管道和 MR 页面中expose the results of your unit tests

如果你使用 PyTest,你需要像这样配置你的 CI 工作:

pytest:
    stage: test
    ...
    before_script:
        - pip install -r requirements.txt
    script:
        - python -m pytest --junitxml=report.xml
    artifacts:
        when: always
        reports:
            junit: report.xml
        expire_in: 2 weeks