使用gitlab-runner执行python unittest后,testcase失败,但是gitlab显示pass。
$ 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中得到结果,怎么办?
可以在 GitLab 的管道和 MR 页面中公开单元测试的结果。
如果您使用 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
您想要捕获结果并对其调用 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)
来源: