我正在为jenkins编写groovy脚本,该脚本在一定程度上定义了我的路径名。我确实尝试过研究stackoverflow和google上的问题,但我想不出一个好的解决方案。
这是一个最小的示例: 文件结构:
runTests.bat
vars/SemVer.groovy
tests/SemVerTests.groovy
批处理文件执行我的单元测试:
docker run --rm -v %cd%:/home/groovy/scripts -w /home/groovy/scripts groovy groovy tests/SemVerTests.groovy
SemVer.groovy
#!/usr/bin/env groovy
package vars
class SemVer {
private String _original
SemVer(String original) { this._original = original }
String toString() { return "${this._original}" }
}
SemVerTests.groovy
import groovy.util.GroovyTestSuite
import junit.framework.Test
import junit.textui.TestRunner
import vars.SemVer
class GetLatestSemVerShouldConstruct extends GroovyTestCase {
void testDisplay() {
def actual = new SemVer("Hello World!").toString()
assertToString(actual, "Hello World!")
} }
class AllTests {
static Test suite() {
def allTests = new GroovyTestSuite()
allTests.addTestSuite(GetLatestSemVerShouldConstruct.class)
return allTests
} }
TestRunner.run(AllTests.suite())
问题是我需要在SemVer.groovy
中的詹金斯脚本中使用vars/
。为此,我假设我需要删除行package vars
。
当我这样做时,我得到:
Compilation incomplete: expected to find the class vars.SemVer in file:/home/groovy/scripts/vars/SemVer.groovy, but the file contains the classes: SemVer
如何在不使用SemVer定义包的情况下将类导入到tests/SemVerTests.groovy
中?
答案 0 :(得分:1)
将vars
添加到类路径(见下文),从package
中删除SemVers
,然后在测试中仅删除import SemVers
。
$ find .
.
./tests
./tests/SemVerTests.groovy
./vars
./vars/SemVer.groovy
$ head -n 4 vars/SemVer.groovy
class SemVer {
private String _original
SemVer(String original) { this._original = original }
String toString() { return "${this._original}" }
$ head -n 8 tests/SemVerTests.groovy
import groovy.util.GroovyTestSuite
import junit.framework.Test
import junit.textui.TestRunner
import SemVer
class GetLatestSemVerShouldConstruct extends GroovyTestCase {
void testDisplay() {
def actual = new SemVer("Hello World!").toString()
$ groovy -cp vars tests/SemVerTests.groovy
.
Time: 0.035
OK (1 test)