You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe your use-case which is not covered by existing documentation.
I'm trying to write unit test to my Jenkins shared library using jenkins-pipeline-unit but for no success.
My project/directory structure is:
src/
└── org
└── company
├── JobData.groovy
├── Constants.groovy
├── Logger.groovy
└── Utils.groovy
vars
└── automationTest.groovy
└── fileIsEmpty.groovy
test
└── AutomationTestTest.groovy
I've initialized gradle and created some build.gradle file with many dependencies and tried to be accurate and use the plugins versions from my jenkins instance.
What I'm trying to do is to write test for automationTest.groovy variable.
I need to test the value of the singleton JobData within the test file but have no idea how to import it
//Utils.groovypackageorg.companyimportgroovy.json.JsonOutputimportorg.yaml.snakeyaml.DumperOptionsimportorg.yaml.snakeyaml.Yamlimportorg.apache.http.client.utils.URIBuilder;
importorg.company.Constantsimportjava.text.SimpleDateFormat;
importjava.util.Date;
importhudson.model.Userimporthudson.tasks.Mailerimportjenkins.model.Jenkinsimportorg.jenkinsci.plugins.pipeline.modeldefinition.UtilsasjUtilsimportorg.company.Loggerimportorg.company.JobDataclassUtils{
// A lot of static classes which uses the imports (like jenkins.model.Jenkins)staticdefyamlToString(Mapyaml){
//implementation here
}
staticListgetTestLogRows(Listloglines, intlineOffset=0){
}
//... (more methods)
}
//JobData.groovypackageorg.companyimportorg.company.Constantsimportorg.company.Utils@SingletonpublicclassJobData{
//singleton for storing job datapublicString releaseBranch
publicdef BAtests = [:]
...
Everytime I run the test and getting undefined classes I struggled with it a lot and the current exceptions is:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: java.lang.NoClassDefFoundError: Unable to load classjenkins.model.Jenkins due to missing dependency antlr/ANTLRException
This is the dependencies block of my build.gradle:
dependencies {
implementation "org.codehaus.groovy:groovy-all:2.4.12"
implementation 'org.codehaus.groovy:groovy-jaxb:3.0.17'
implementation 'javax.servlet:javax.servlet-api:4.0.1@jar'
implementation 'org.jenkins-ci.main:remoting:3.40@jar'
implementation 'org.springframework.security:spring-security-core:5.5.2'
implementation 'com.thoughtworks.xstream:xstream:1.4.16'
implementation 'org.antlr:antlr-runtime:3.5.2@jar'
implementation 'antlr:antlr:3.0ea8'
implementation 'org.kohsuke.stapler:stapler:1777.v7c6fe6d54a_0c@jar'// testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
testImplementation "com.lesfurets:jenkins-pipeline-unit:1.9"
testImplementation "junit:junit:4.12"//Fetch 3rd imports for src/utils class
testImplementation 'org.yaml:snakeyaml:1.28'
testImplementation 'org.apache.httpcomponents:httpclient:4.5.13@jar'
testImplementation 'org.jenkins-ci.main:jenkins-core:2.303.2@jar'
testImplementation 'org.jenkinsci.plugins:pipeline-model-api:1.9.2@jar'
testImplementation 'org.jenkinsci.plugins:pipeline-model-definition:1.9.2@jar'
testImplementation 'org.jenkinsci.plugins:pipeline-model-extensions:1.9.2@jar'
implementation 'org.jenkinsci.plugins:pipeline-model-declarative-agent:1.1@jar'
testImplementation 'org.connectbot.jbcrypt:jbcrypt:1.0.0@jar'
testImplementation 'org.jenkins-ci.plugins:mailer:408.vd726a_1130320@jar'
}
And this is the test that I'm trying to write:
//test/AutomationTestTest.groovy//Should I?//package org.company//import org.company.JobDataimportorg.junit.*importcom.lesfurets.jenkins.unit.*import staticgroovy.test.GroovyAssert.*//Imports for loading the library itself import staticcom.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.libraryimport staticcom.lesfurets.jenkins.unit.global.lib.ProjectSource.projectSourceclassAutomationTestTestextendsBasePipelineTest {
Object script
def automationTest
def resourcesTestDir
@BeforevoidsetUp(){
super.setUp()
//This fails due to unsatisfied dependencies in Utils.groovyObject library = library()
.name('jenkins-shared-lib')
.defaultVersion('<notNeeded>')
.allowOverride(true)
.implicit(true)
.targetPath('<notNeeded>')
.retriever(projectSource())
.build()
helper.registerSharedLibrary(library)
script = loadScript('test/resources/EmptyPipeline.groovy')
assertNotNull(script)
resourcesTestDir =newFile('test/resources/AutomationTest')
automationTest = loadScript("vars/automationTest.groovy")
}
@TestvoidtestBeafBAtest(){
helper.addShMock(~'''(?m)#!/bin/bash #Create and activate virtualenv #python3.6 -m venv venv .*''', '', 0)
helper.addShMock('ls -1 pytests/log_*.ba || true', baLogs.join('\n') , 0)
helper.addShMock('exit \$(cat pytests/exit_status)', '', 0)
//Mock log files from test/resources/AutomationTest such as ['log_0.ba', 'log_1.ba']
resourcesTestDir.eachFileMatch(~/log_\d+\.ba/){ baLogFile->
helper.addReadFileMock(baLogFile.name, baLogFile.text)
}
automationTest.beafBAtest("HVS180", "some-test-file")
//How should I verify that JObData.instance.BAtests has the correct value
}
My questions are:
How can I manage to run the test pass the missing dependencies such as missing dependency antlr/ANTLRException, is there any chance to fill gradle dependencies automatically?
Am I doing something wrong? how the test should be done? how should I check the singleton content at the end of the test?, should I put the test inside package org.company?
The text was updated successfully, but these errors were encountered:
The Jenkinsfile works and the job runs as expected. Now to test the pipeline and jenkinsfile I dont see a way to mock the classes part of src/gradlecheck that is used by library gradleCheckFlakyTestChecker part of jenkinsfile. This throws an error when tested as
Describe your use-case which is not covered by existing documentation.
I'm trying to write unit test to my Jenkins shared library using jenkins-pipeline-unit but for no success.
My project/directory structure is:
I've initialized gradle and created some build.gradle file with many dependencies and tried to be accurate and use the plugins versions from my jenkins instance.
What I'm trying to do is to write test for automationTest.groovy variable.
here is some relevant code:
I need to test the value of the singleton JobData within the test file but have no idea how to import it
And this is the test that I'm trying to write:
My questions are:
The text was updated successfully, but these errors were encountered: