首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在gitlab-ci上获得单元测试结果?

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

Stack Overflow用户
提问于 2019-07-08 13:25:35
回答 2查看 3K关注 0票数 0

使用gitlab-runner执行python unittest后,testcase失败,但gitlab显示通过。

代码语言:javascript
复制
$ 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配置文件如下:

代码语言:javascript
复制
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测试/测试-utils.py这个命令是不成功的,并且没有真正得到unittest的结果,该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2021-03-17 08:58:23

可以在GitLab的流水线和MR页面中使用expose the results of your unit tests

如果您使用的是PyTest,则需要按如下方式配置CI作业:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2021-03-12 03:38:18

如果您使用TextTestRunner将测试作为一个模块运行,那么它不会退出并返回代码1。

您可能希望捕获结果并对其调用wasSuccessful(),如下所示:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56928856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档