gpt4 book ai didi

java - 即使测试失败,如何始终在 Jenkins Groovy 管道中写入 .csv 数据文件

转载 作者:行者123 更新时间:2023-12-04 09:25:50 25 4
gpt4 key购买 nike

我有一个使用 TestNG 生成新客户数据的测试:

public class GenerateNewCustomers{

private static ArrayList<String> customers = new ArrayList<>();

@Test(invocationCount = 5)
public void runApplicationGeneration() throws InterruptedException {

api = new AgreementApiCalls();
api.createNewCustomer();//more api calls to enrich customer object
api.customers = api.insertNewcustomer();

setCustomers(customers);

@AfterClass
public void writeTestData() throws IOException {
generateTestDataFile();
}

private static void setCustomers(ArrayList<String> customers) {
GenerateNewCustomers.customers = customers;
}

private void generateTestDataFile() throws IOException {

FileWriter writer = new FileWriter("src/test/resources/testData.csv");

List<String> test = new ArrayList<>(customers);

String collect = test.stream().collect(Collectors.joining(","));

writer.write(collect);
writer.close();
}
这在本地工作正常:- 如果出于参数的考虑,调用 4 失败,则前 3 次仍会写入文件 testData.csv .
我的问题是我如何从 CI 的角度重现这种行为,特别是 Jenkins?当我从 CI 运行时,任何失败都会导致文件未被写入。我的管道脚本有这个:-
env.PASSWORD_TOOL_SECRET = 'password'
env.PASSWORD_TOOL_SESSION = ''

String getOutputFromClientTool( String command ) {
String output = sh script: command, returnStdout: true
output.trim()
}

def setEnvironmentVariables () {
sh "java -version"
env.PASSWORD_TOOL_SESSION = getOutputFromClientTool('./configuration/getPasswordToolSession.sh')
sh "op get item 'run-tests-${env.ENVIRONMENT}' --vault='my-team' --session=${env.PASSWORD_TOOL_SESSION} > run-tests.json"

env.SYS_USERNAME = getOutputFromClientTool("cat run-tests.json | jq -r '.details.sections[] | select( .fields != null ) | .fields[] | select( .t == \"sys-username\" ).v'")
env.SYS_PASSWORD = getOutputFromClientTool("cat run-tests.json | jq -r '.details.sections[] | select( .fields != null ) | .fields[] | select( .t == \"sys-password\" ).v'")...//more env vars set here


withCredentials([
usernamePassword(
credentialsId: 'maven-ro',
usernameVariable: 'MAVEN_USERNAME',
passwordVariable: 'MAVEN_PASSWORD'
)
]) {
env.MAVEN_USERNAME = MAVEN_USERNAME
env.MAVEN_PASSWORD = MAVEN_PASSWORD
}
}
try {
sh "./gradlew clean test -Denv=${env.ENV} -
PtestNGSuite=${suite} --no-daemon"
archiveArtifacts artifacts:
"src/test/resources/testData.csv", fingerprint: true
} finally {
publishHTML(
target: [
allowMissing : false,
alwaysLinkToLastBuild: false,
keepAll : true,
reportDir : 'build/reports/tests/test',
reportFiles : 'index.html',
reportName : "Customer Generation Report"
]
)
}
}
}

} catch (exception) {
currentBuild.result = 'FAILURE'
throw exception
} finally {
// configure notification to slack channel
}

最佳答案

我想你错过了 archiveArtifacts如果有异常(exception)。尝试将其移至合适的位置 post条款,像这样:

pipeline {
stages {...}

post {
always {
archiveArtifacts artifacts: "src/test/resources/testData.csv", fingerprint: true
}
}
}

关于java - 即使测试失败,如何始终在 Jenkins Groovy 管道中写入 .csv 数据文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63011928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com