Unit testing 当包和文件夹发生偏差时,如何在groovy中导入类?
我正在为jenkins编写groovy脚本,它在某种程度上定义了我的路径名。我曾尝试在stackoverflow和google上重新搜索这个问题,但我没有找到一个好的解决方案 下面是一个简单的例子: 文件结构:Unit testing 当包和文件夹发生偏差时,如何在groovy中导入类?,unit-testing,groovy,import,jenkins-groovy,Unit Testing,Groovy,Import,Jenkins Groovy,我正在为jenkins编写groovy脚本,它在某种程度上定义了我的路径名。我曾尝试在stackoverflow和google上重新搜索这个问题,但我没有找到一个好的解决方案 下面是一个简单的例子: 文件结构: runTests.bat vars/SemVer.groovy tests/SemVerTests.groovy 批处理文件执行我的单元测试: docker run --rm -v %cd%:/home/groovy/scripts -w /home/groovy/scripts gr
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())
问题是我需要在vars/
中的jenkins脚本中使用SemVer.groovy
。为此,我假设需要删除行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
如何将类导入到
tests/SemVerTests.groovy
,而不在SemVer中定义包?将vars
添加到类路径(见下文),从SemVers
中删除包,然后在测试中只导入SemVers
$find。
.
/测试
./tests/SemVerTests.groovy
/瓦
./vars/SemVer.groovy
$head-n 4个变量/SemVer.groovy
SemVer类{
私有字符串
SemVer(字符串原件){this.\u original=original}
字符串toString(){返回“${this.\u original}”}
$head-n 8个测试/SemVerTests.groovy
导入groovy.util.GroovyTestSuite
导入junit.framework.Test
导入junit.textui.TestRunner
进口塞姆弗
类GetLatestSemVerShouldConstruct扩展了GroovyTestCase{
void testDisplay(){
def actual=new SemVer(“Hello World!”).toString()
$groovy-cp vars测试/SemVerTests.groovy
.
时间:0.035
正常(1次测试)