我想看到stdout来自jenkins构建日志中的python测试。我正在sh脚本中的Jenkins管道中运行pytest(== 5.3.1):
stage('unit tests') {
print "starting unit tests"
sh script: """
source env-test/bin/activate && \
python -m pytest -x -s src/test/test*.py
""", returnStdout: true, returnStatus: true
}
请注意,我正在使用虚拟环境(env-test)运行测试。不幸的是,Jenkins日志不显示我在测试中发送的输出:
def test_it(self):
print('\nhello world')
self.assertTrue(True)
但是它仅显示初始呼叫:
+ python -m pytest -x -s src/test/testModel.py
[Pipeline] }
[Pipeline] // stage
而我的本地pycharm ide和gitbash显示所有输出:
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-5.3.1, py-1.8.0, pluggy-0.13.1 -- C:\...\Anaconda3\python.exe
cachedir: .pytest_cache
rootdir: C:\...\src\test
collecting ... collected 1 item
testModel.py::TestModel::test_it
PASSED [100%]
hello world
============================== 1 passed in 0.57s ==============================
pytest文档正在谈论Capturing of the stdout/stderr output。因此,我尝试使用-s参数来禁用捕获,但没有成功。
问题是groovy sh脚本命令的returnStdout parameter:
returnStdout(可选),如果选中,则任务的标准输出为作为步骤值作为字符串返回,而不是打印到构建日志。 (如果有标准错误,仍然会打印到log。)您通常会希望在结果上调用.trim()来剥离尾随换行符。类型:布尔值
所以我只需从sh脚本命令中删除该选项。