当 pytest 中有失败用例,那么 Jenkins 会构建失败,下面的 log 如下:

=========================== short test summary info ===========================
FAILED test_h.py::test_hi - assert 1 == 4
======================== 1 failed, 1 warning in 0.34s =========================
Post stage
[Pipeline] allure
[pipeline] $ F:\allure2-master\allure-2.13.9\bin\allure.bat generate -c -o C:\Users\user\.jenkins\workspace\pipeline\allure-report
allure-results does not exist
Report successfully generated to C:\Users\user\.jenkins\workspace\pipeline\allure-report
Allure report was successfully generated.
Creating artifact for the build.
Artifact was added to the build.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

看不出原因啊,然后把 pytest 中用例改为必定会成功的用例,则 Jenkins 会构建成功。

开始猜测:难道 pytest+allure+jenkisn 必须执行的用例是成功用例才行?可是我记得 Jenkins 构建成不成功应该不取决于测试用例本身的,这是怎么回事呢?

更新一下 pipeline:

pipeline {
    agent any
    environment{
        HHH = "lalalala"
    }
    stages {
        stage('Hello') {
            steps {
                 bat '''cd C:\\Users\\user\\PycharmProjects\\smarthome
pytest -m aaa --alluredir="./outputs/reports" --clean-alluredir --cache-clear
if exist ".pytest_cache/v/cache/lastfailed" (
    pytest -m aaa --lf --alluredir="./outputs/reports"
) else (
    echo "xxx"
)'''
            }
            post{
                always{
                    allure jdk: 'JDK', results: [[path: "./outputs/reports"]]
                }
                success{
                    emailext attachLog: true, attachmentsPattern: 'allure-report/index.html', body: '''<html>
<head>
<meta charset="UTF-8">
<title>${PROJECT_NAME}-第${BUILD_NUMBER}次构建日志</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本邮件是程序自动下发的,请勿回复!)</td>
        </tr>
        <tr>
            <td><h2>
                    <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称 : ${PROJECT_NAME}</li>
                    <li>构建编号 : 第${BUILD_NUMBER}次构建</li>
                    <li>触发原因: ${CAUSE}</li>
                    <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>构建  Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>项目  Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="    %p"}
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">构建日志 :</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG,maxLines=10000}</textarea>
            </td>
        </tr>
    </table>
</body>
</html>''', recipientProviders: [buildUser()], subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: 'lishengjiao_13@163.com'
                }
            }
        }
    }
}


↙↙↙阅读原文可查看相关链接,并与作者交流