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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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 我可以从命令行为Grails项目运行单个单元测试吗?_Unit Testing_Grails - Fatal编程技术网

Unit testing 我可以从命令行为Grails项目运行单个单元测试吗?

Unit testing 我可以从命令行为Grails项目运行单个单元测试吗?,unit-testing,grails,Unit Testing,Grails,我一直在通过键入运行所有单元测试的grailstest-app:unit来运行我的Grails单元测试。有没有办法指定单个测试 编辑:到目前为止,每个人基本上都在说同样的话,但当我这么做时,没有运行任何测试。还有什么想法吗 结论:好的,我使用的是测试类的名称,而不是被测试类的名称。有一次我尝试了Foo而不是FooTests,效果非常好。是的,有 grails test-app -unit YourController.testSomething 其中控制器是控制器,testSomething是

我一直在通过键入运行所有单元测试的
grailstest-app:unit
来运行我的Grails单元测试。有没有办法指定单个测试

编辑:到目前为止,每个人基本上都在说同样的话,但当我这么做时,没有运行任何测试。还有什么想法吗

结论:好的,我使用的是测试类的名称,而不是被测试类的名称。有一次我尝试了
Foo
而不是
FooTests
,效果非常好。

是的,有

grails test-app -unit YourController.testSomething
其中控制器是控制器,testSomething是测试方法

你应该看到类似的东西

测试通过-在中查看报告

grails test-app -unit com.package.YourController.testSomething

运行测试时需要包含包名。给定测试类foo.BarTests,可以使用以下命令仅运行该类中的测试:

grails test-app :unit foo.Bar
或在该类中运行单个测试方法,使用:

grails test-app :unit foo.Bar.testMethod

请注意,在指定测试类的名称时,不包括“测试”一词。

设置可能出错的可能性:

  • 你的命令不正确。对我有效的是:

    grails测试应用程序-unitfoo
    (其中我的测试类是
    FooTests.groovy

  • 您没有显式导入
    grails.test.GrailsUnitTestCase

    当我没有导入它时,我在识别我的测试时遇到了一些问题。当我扩展
    GroovyTestCase
    时,一切似乎正常

工作示例 下面是一组对我有用的测试示例。也许你能发现它们和你的测试之间的一些差异

注意:这些都是在安装了
测试
插件的情况下运行的

测试/单元/足部测试。groovy

import grails.test.GrailsUnitTestCase
class FooTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
import grails.test.GrailsUnitTestCase
class BarTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
package my.pkg

import grails.test.GrailsUnitTestCase

class BazTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
测试/单元/易货测试。groovy

import grails.test.GrailsUnitTestCase
class FooTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
import grails.test.GrailsUnitTestCase
class BarTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
package my.pkg

import grails.test.GrailsUnitTestCase

class BazTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
test/unit/my/pkg/BazTests.groovy

import grails.test.GrailsUnitTestCase
class FooTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
import grails.test.GrailsUnitTestCase
class BarTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
package my.pkg

import grails.test.GrailsUnitTestCase

class BazTest extends GrailsUnitTestCase {
    void testFoo() {
        assert true
    }

    void testBar() {
        assert true
    }
}
命令:所有单元测试

$ grails test-app -unit
...

Starting unit test phase ...

-------------------------------------------------------
Running 6 unit tests...
Running test my.pkg.BazTest...PASSED
Running test FooTest...PASSED
Running test BarTest...PASSED
Tests Completed in 847ms ...
-------------------------------------------------------
Tests passed: 6
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit Foo
...

Starting unit test phase ...

-------------------------------------------------------
Running 1 unit test...
Running test FooTest...PASSED
Tests Completed in 815ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit my.pkg.Baz
...

Starting unit test phase ...

-------------------------------------------------------
Running 2 unit tests...
Running test my.pkg.BazTest...PASSED
Tests Completed in 842ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
命令:Foo单元测试

$ grails test-app -unit
...

Starting unit test phase ...

-------------------------------------------------------
Running 6 unit tests...
Running test my.pkg.BazTest...PASSED
Running test FooTest...PASSED
Running test BarTest...PASSED
Tests Completed in 847ms ...
-------------------------------------------------------
Tests passed: 6
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit Foo
...

Starting unit test phase ...

-------------------------------------------------------
Running 1 unit test...
Running test FooTest...PASSED
Tests Completed in 815ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit my.pkg.Baz
...

Starting unit test phase ...

-------------------------------------------------------
Running 2 unit tests...
Running test my.pkg.BazTest...PASSED
Tests Completed in 842ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
命令:my.pkg.Baz单元测试

$ grails test-app -unit
...

Starting unit test phase ...

-------------------------------------------------------
Running 6 unit tests...
Running test my.pkg.BazTest...PASSED
Running test FooTest...PASSED
Running test BarTest...PASSED
Tests Completed in 847ms ...
-------------------------------------------------------
Tests passed: 6
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit Foo
...

Starting unit test phase ...

-------------------------------------------------------
Running 1 unit test...
Running test FooTest...PASSED
Tests Completed in 815ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports
$ grails test-app -unit my.pkg.Baz
...

Starting unit test phase ...

-------------------------------------------------------
Running 2 unit tests...
Running test my.pkg.BazTest...PASSED
Tests Completed in 842ms ...
-------------------------------------------------------
Tests passed: 2
Tests failed: 0
-------------------------------------------------------

...
Tests PASSED - view reports in target/test-reports

我在Grails1.2.3和Grails1.3.4中尝试了这些,它们的表现都是一样的。

这肯定会奏效,我每天都在使用它

下面将运行一个测试

grails test-app :unit ExampleControllerTests.testName
下面将运行所有ExampleControllerTests

grails test-app :unit ExampleControllerTests

嗯。不为我工作。它试图运行测试,但是说通过了,找不到。我猜,还是不起作用。如果它是一个服务类,而不是像控制器那样生成的,这会有区别吗?OP特别指出,使用测试类的名称是他的错误,但这里的示例正好说明了这一点。另外,你的语法看起来很可疑,后面是冒号而不是前导破折号,虽然我真的不知道……如果你真的不知道,请不要评论并投反对票,这是完全正确的。使用测试类的名称根本没有问题,我每天都使用它,我知道它是有效的。(我使用Grails1.3.7)我不知道如何调和你所说的和OP所说的是最终答案。否决票并不是因为语法本身我会忽略它。嗯,这在Grails2.0中适用。我很好奇17个月前我遇到这种困难时用的是什么版本@埃里克,我建议你倒转你的投票,因为这个答案是正确的,尽管我以前的断言是正确的。@Eric谢谢你提供的信息!除了否决投票之外,我还发表了评论,以便有机会解决问题——如果问题存在的话。现在,这为人们提供了用新信息更新答案或问题的机会。如果我能帮上忙,我决不会不公平地投反对票。各位,请相信这个过程。:)你的版本是什么?我相信这个特性是在1.2版本中引入的。您可以发布您正在输入的实际命令行和输出吗?正如您从答案中看到的,这应该是可行的,因此在实际键入命令之前的某个地方存在问题。例如,测试的名称是什么?testcase中方法的名称是什么?第一条评论有误导性,您可以使用Grails 1.1.1测试单个单元测试这是我使用Grails 3.3.6测试单个方法的唯一方法。