Parametrize JUnit test in Gradle build - jenkins

I need to inject some paths and URLs to Gradle managed JUnit tests (like APK file path on remote host and Appium server URL) because local environment differ from CI and there are several CI envs...
How can I inject settings into tests (as strings)? I see the use of Sysyem.getenv() included in static blocks to bypass Gradle smartness. I can set variables before calling Gradle:
USE_SERVER=http://mobi-tester.local gradle testServerApiOnAndroid
I don't see the way to get it works with system properties, unless use something like:
tasks.withType(JavaExec) {
systemProperties System.properties
// or
systemProperty "myvariable", System.getProperty("myvariable")
}
Is there any declarative way to inject strings into variables with pure JUnit?
Please, don't suggest https://github.com/junit-team/junit4/wiki/Parameterized-tests - you just haven't got the question right ((

Related

Best way to store the deployment path in jenkins

I am creating jenkins pipeline for all our application where I wanted to build and deploy. I can able to achieve that but all the deployment paths are hard coded on the pipeline script.
We have around 8 application and 5 environments. it means I need to specify 40 different deployable path on the pipeline scripts.
I like to know, are they any best way to store the deployment path?. I thought about storing them in XML and reading that while doing the build, but not sure on implementation.
looking for some ideas.
script {
def msbuild tool name: 'Msbuila', type: 'msbuild'
def action "${msbuild}\\msbuild.exe"
def rootPath "${NORKSPACE}\\test\\test";
def sinPath "${rootPath}\\test.sin"
def binPath "${rootPath}\\test\\bin"
bat “nuget restore \"${sinPath}\""
bat "\"${action}\" \"${sinPath)\" "
robocopy("\"${binPath}\" \"\\\\t.test.com\\test\" /MIR /xF ")
}
What I would do is use a config repository, having it configured this way:
Each application is a different repository (example: app_config)
Each environment is a different file
The same enviroment file in each repository is called by the same name
Each enviroment file is a yaml (key:value)
Then on the jenkins pipeline I would get the repo, read the yaml using readYAML (check the command usage and name, theres is a while since I used it) and load it on a map.
Then you use the variables of the map and that should help you
The tricky part is how to match the code repositories and the config repositories. As I mentioned before, I would use the same name and append "_config"

Howto set up classpath for System Groovy Script in Jenkins

Documentation for the Groovy Plugin of Jenkins states that
The system groovy script, OTOH, runs inside the Jenkins master's JVM.
Thus it will have access to all the internal objects of Jenkins, so
you can use this to alter the state of Jenkins. It is similar to the
Jenkins Script Console functionality.
Yet I find that I have a groovy script that I can successfully run in Jenkins Script Console but which does NOT run if entered as a "System Groovy Script" on a build configuration. There are compiler errors. Clearly, the Jenkins Script Console is running with a different classpath than the script in my build. But I can't find information on what the default classpath is when running a script for a build or what the classpath is when running from the Script Console, so I might duplicate that for my script.
Also, the plugin offers a classpath entry field for running the script as a file but that option does not exist for entering the script as text.
I can't get my script to work either way.
What am I missing?
I think the answer is that the Script Console auto-imports the whole Jenkins library. That is not the case with the System Groovy Script. So what worked for me was to run the script, and for every compiler error about an unknown class, add an import statement for that class. I learned what packages they were from by looking at Javadocs.
Automating this would be a nice improvement to the plugin.
May be use the grab dependency management to resolve the library to add

How to pass parameter in the Cucumber Runner file to run files from specific folder

I am using Serenity Cucumber framework using Gradle. I have integrated this with Jenkins. My requirement is that as a part of one Jenkins job I would like to run the feature files from one folder and as a part of next Jenkins job feature files located from other folder should get executed. Can you please suggest how I can pass the parameters to Cucumber Runner file during run time. For e.g.:
Below is my feature file
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(features = "src/test/resources/Sanity/")
public class TestRunnerSerenity {
}
As a part of Sanity Build all my test cases from src/test/resources/Sanity/ folder should get executed. So how I can pass this folder path value to the Cucumber Runner class run time. So that I can maintain my builds without having multiple runner files and manual intervention.
Thanks in advance for your help.
There is some configuration need to do in Code,Gradle and Jenkins and Here is step by step
In Grade file, I will create systemProperty function.
task prodTest(type: Test)
prodTest{
systemProperty 'test.folder', testFolder
}
task "runProdTest" {
dependsOn "clean", "cleanTest", "prodTest"
}
In Constants Class,
Create static final String
public static final String TEST_FOLDER = system.getProperty("test.folder","FolderName")`
When you run locally, It will not failed because of external parameter is not available.
Go to runner file,
#RunWith(CucumberWithSerenity.class)
#CucumberOptions(features = Constants.TEST_FOLDER)
public class TestRunnerSerenity {
}
Now Jenkins Configuration
Go to Jenkins - Configuration
There should be Invoke Gradle Script Script. And You can add parameter under "switches". You will tell jenkis to u
-Ptest.folder="src/test/resources/Sanity/"
Tasks = runProdTest
You all set !!

Configure Jenkins to run Visual Studio bat file before Maven job

I have a Maven module that utilizes the NAR plugin to build some JNI libraries, and in Jenkins I have configured a Maven project to build this module. In order for these libs to be built, the Visual Studio bat file that sets up the path and other environment variables must be run. I have tried several different ways to get this bat file to execute before the Maven commands are called, and none of them have worked correctly. I know that Jenkins isolates all of the build process steps, so it can be difficult to get the environment set up, but I'm hoping someone has solved this particular issue. Here's what I've tried:
calling the bat file as a pre build step.
Using the EnvInject plugin to call the bat file, both as a pre build step and as a pre job step.
Setting the environment variables directly without calling the bat file using EnvInject.
calling the bat file from the mvn.bat file (this failed because it appears Jenkins will call Maven directly, without using the bat file).
As a workaround, I'm using the Freeform project type and setting the build steps to
call the bat file.
Directly call maven with appropriate parameters.
This works, but it's not as nice as using the Maven project type, ex. a failed unit test will fail the entire build instead of just sending a warning. Is there a way to configure this as a Maven project?

how to scan test source code with the SonarQube Jenkins plugin?

I'm trying to make a Jenkins job that only scans the test source files, so everything under /src/test/java (using Maven). I use the SonarQube Jenkins post-action for this.
When we used to configure Sonar in the pom file directly we could do this in a profile:
<sonar.sources>/src/test/java</sonar.sources>
<sonar.tests/>
That worked fine.
But in the Jenkins job I have to specify these as 'Additional properties' and I can't seem to specify an emtpy sonar.tests element. I tried -Dsonar.tests, -Dsonar.tests=,-Dsonar.tests="", nothing works. When this element is not empty Sonar will attempt to scan the test files twice and crash.
The post-build step is specifically and explicitly a Maven operation. Your problem comes from trying to use Maven to do something un-Mavenish; i.e. ignore the convention that files in the tests directory should be treated as tests.
Since you want to scan your tests as code, your best bet is to use the build step (which uses SonarQube Scanner) and set your scanner properties manually. That will make it easy to set your sources directory and to omit the tests directory.

Resources