[英]How do I detect my test is running on a Jenkins environment?
I have a JUnit test using Assumption
to skip the test if the developer's computer doesn't have the pre-requisite software for running it. 如果开发人员的计算机没有运行它的必备软件,我使用
Assumption
跳过测试的JUnit测试。 Despite being "junit", it's an integration test. 尽管是“junit”,但它是一个集成测试。 Something like this:
像这样的东西:
int isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor();
Assume.assumeThat("Software not present", isSoftwarePresent, is(equalTo(0)));
However, at one point I realized the test had stopped running on the automated build on Jenkins, due to that assumption, and eventually a regression was introduced which the test was supposed to stop. 但是,有一次我意识到测试已经停止在Jenkins的自动构建上运行,由于这个假设,并且最终引入了测试应该停止的回归。
To put in other words, the required software went missing from Jenkins slave environment, which caused the test to be skipped. 换句话说,Jenkins从属环境中缺少所需的软件,导致测试被跳过。
The automated test is run by maven with the FailSafe plugin, on a Jenkins Pipeline build plan. 在Jenkins Pipeline构建计划中,maven使用FailSafe插件运行自动化测试。 How can I detect that my environment is Jenkins so that I can make the assumption condition more strict?
如何检测我的环境是Jenkins,以便我可以使假设条件更严格?
That is, I want the condition to be something like this: 也就是说,我希望条件是这样的:
boolean isJenkinsBuild = /* true if this is being run inside a Jenkins build, false otherwise */;
boolean isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor() == 0;
Assume.assumeTrue("Software not present", isSoftwarePresent || isJenkinsBuild);
Or even, 甚至,
@Test
void testJenkinsEnvironment() {
...
Assume.assumeTrue(isJenkinsBuild);
Assert.assertTrue(isSoftwarePresent);
}
@Test
void testFeature() {
...
Assume.assumeTrue(isSoftwarePresent);
...
}
Since they are easy to test for and passed to every program that gets executed by the build, I opted to base the check on environment variables. 由于它们很容易测试并传递给构建执行的每个程序,因此我选择基于环境变量进行检查。
Though rohit answer shows one way of setting variables on Jenkinsfile, I'd rather rely on things that Jenkins itself does, so I checked the environment variables set on my Jenkins jobs, and there's many options to pick from: 虽然rohit回答显示了在Jenkinsfile上设置变量的一种方法,但我宁愿依赖Jenkins本身所做的事情,所以我检查了我的Jenkins作业上设置的环境变量,并且有很多选择可供选择:
These seem to be constant on a Jenkins instance. 这些似乎在Jenkins实例上是不变的。
HUDSON_URL
JENKINS_URL
Not Jenkins-specific, but the following is also useful: 不是Jenkins特有的,但以下内容也很有用:
USER
Our Jenkins runs under user jenkins
, so testing that value is also a possibility. 我们的Jenkins在用户
jenkins
下运行,因此测试该值也是可能的。
These have constant values across builds for the same job. 对于同一作业,它们在构建中具有常量值。
JOB_URL
JOB_NAME
JOB_BASE_NAME
JOB_DISPLAY_URL
These have constant values across the same build (that is, for different sh
invocations). 它们在同一个构建中具有常量值(即,对于不同的
sh
调用)。
BUILD_URL
BUILD_TAG
RUN_CHANGES_DISPLAY
RUN_DISPLAY
BUILD_DISPLAY_NAME
BUILD_ID
BUILD_NUMBER
These are related to the node on which the current command is being executed. 这些与正在执行当前命令的节点有关。
JENKINS_HOME
JENKINS_NODE_COOKIE
NODE_LABELS
NODE_NAME
This one changes with each build, may change from node to node, and may even change with Jenkinsfile configuration: 这个随每个构建而变化,可能会在节点之间变化,甚至可能随Jenkinsfile配置而变化:
WORKSPACE
I'm not sure about these. 我不确定这些。 They are definitely from Jenkins, but I don't know what their lifecycle is.
他们绝对来自詹金斯,但我不知道他们的生命周期是什么。
HUDSON_SERVER_COOKIE
JENKINS_SERVER_COOKIE
HUDSON_COOKIE
For my own purposes, I decided to go with JENKINS_HOME
. 出于我自己的目的,我决定和
JENKINS_HOME
一起去。 The name is very Jenkins-specific, and it seems more fundamental than, say, JENKINS_URL
. 这个名字非常特定于Jenkins,它似乎比
JENKINS_URL
更基础。 While it doesn't imply the build is being run by Jenkins, it will always be set in that case. 虽然它并不意味着构建是由Jenkins运行的,但在这种情况下它总是会被设置。 I don't mind false positives, as long as I don't get false negatives.
我不介意误报,只要我没有得到假阴性。
Couple of ways you can achieve this: 有几种方法可以达到这个目的:
You can make you application accept arguments and then pass a TRUE/FALSE value indicating it is running from Jenkins or not. 您可以使应用程序接受参数,然后传递一个TRUE / FALSE值,表明它是从Jenkins运行的。
You can also read the system properties of os 您还可以读取操作系统的系统属性
eg System.getProperty("os.arch");
例如System.getProperty(“os.arch”);
But this will not work if your Jenkins environment and your workspace are on the same machine 但是,如果您的Jenkins环境和工作区位于同一台计算机上,则无法使用此功能
Like so : 像这样:
Pipeline- option1 Pipeline- option1
pipeline {
environment {
FROM_JENKINS= "TRUE"
} stage('test'){
sh "mvn test"
}
}
Pipeline- option2 管道 - 选项2
pipeline {
stage('test'){
sh '''FROM_JENKINS="TRUE" // setting the env variable in the same shell where you are running mvn
mvn test'''
}
}
Application 应用
boolean isJenkinsBuild = Boolean.valueOf(System.getenv("FROM_JENKINS"));
boolean isSoftwarePresent = new ProcessBuilder("check software presence").start().waitFor() == 0;
Assume.assumeTrue("Software not present", isSoftwarePresent || isJenkinsBuild);
Hope it helps :) 希望能帮助到你 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.