Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 当包和文件夹发生偏差时,如何在groovy中导入类?_Unit Testing_Groovy_Import_Jenkins Groovy - Fatal编程技术网

Unit testing 当包和文件夹发生偏差时,如何在groovy中导入类?

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

我正在为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())
问题是我需要在
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次测试)