我可以从命令行为Grails项目运行单个单元测试吗? [英] Can I run a single unit test from the command line for a Grails project?

查看:105
本文介绍了我可以从命令行为Grails项目运行单个单元测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过输入 grails test-app:unit 来运行我的Grails单元测试,它运行所有的单元测试。有没有办法指定一个测试?



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



结论:好的,我使用的是测试类的名称,而不是被测试类的名称。一旦我尝试 Foo 而不是 FooTests ,它就完美了。

li>

您的命令顺序不正确。这对我来说很有用:

grails test-app -unit Foo (其中我的测试类是<$ c

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

  • / code>。



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




    < h2>工作示例

    以下是一组适用于我的测试示例。也许你可以发现他们和你的测试之间的一些区别?



    注意:这些都是在测试插件安装



    test / unit / FooTests.groovy

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

    void testBar(){
    assert true
    }
    }

    test / unit / BarTests.groovy

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

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



    / strong>

     包my.pkg 

    导入grails.test.GrailsUnitTestCase

    class BazTe st扩展GrailsUnitTestCase {
    void testFoo(){
    assert true
    }

    void testBar(){
    assert true
    }

    命令:所有单元测试

      $ grails test-app -unit 
    ...

    启动单元测试阶段...

    --------------------------------------------- ----------
    运行6个单元测试...
    运行测试my.pkg.BazTest ...通过
    运行测试FooTest ...通过
    正在运行测试BarTest ...已通过
    在847ms内完成测试...
    --------------------------- ----------------------------
    测试通过:6
    测试失败:0
    - -------------------------------------------------- ---

    ...
    测试通过 - 在目标/测试报告中查看报告

    命令:Foo单元测试

      $ grails test-app  - 单位Foo 
    ...

    启动单元测试阶段...

    ------------------------------------------ -------------
    运行1个单元测试...
    运行测试FooTest ...通过
    测试在815ms完成...
    -------------------------------------------------- -----
    测试通过:2
    测试失败:0
    ------------------------- ------------------------------

    ...
    测试已通过 - 查看报告in target / test-reports

    命令:my.pkg.Baz单元测试

      $ grails test-app -unit my.pkg.Baz 
    ...

    启动单元测试阶段...

    --------------------------------- ----------------------
    运行2个单元测试...
    运行测试my.pkg.BazTest ...通过
    测试已完成842ms ...
    ------------------------------------- ------------------
    测试通过:2
    测试失败:0
    ------------ -------------------------------------------

    ...
    测试已通过 - 在目标/测试报告$中查看报告b $ b

    我在Grails 1.2.3和Grails 1.3.4中尝试了这些,表现都一样。


    I've been running my Grails unit tests by typing grails test-app :unit, which runs all unit tests. Is there a way to specify a single test?

    Edit: So far, everyone is basically saying the same thing, but when I do that, no tests are run. Any more thoughts?

    Conclusion: OK, I was using the name of the test class, rather than the name of the class being tested. Once I tried Foo instead of FooTests it worked perfectly.

    解决方案

    Possibilities of things that might be wrong with your setup:

    • Your command order is incorrect. What works for me is:

      grails test-app -unit Foo (where my test class is FooTests.groovy)

    • You aren't explicitly importing grails.test.GrailsUnitTestCase.

      I had some problems with it recognizing my tests when I didn't import this. When I was extending GroovyTestCase, things seemed to work normally.

    Working Example

    Here's a sample set of tests that work for me. Perhaps you can spot some differences between them and your tests?

    Note: These are all run with the testing plugin installed

    test/unit/FooTests.groovy

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

    test/unit/BarTests.groovy

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

    test/unit/my/pkg/BazTests.groovy

    package my.pkg
    
    import grails.test.GrailsUnitTestCase
    
    class BazTest extends GrailsUnitTestCase {
        void testFoo() {
            assert true
        }
    
        void testBar() {
            assert true
        }
    }
    

    command: all unit tests

    $ 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
    

    command: Foo unit tests

    $ 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
    

    command: my.pkg.Baz unit tests

    $ 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
    

    I tried these in Grails 1.2.3 and Grails 1.3.4, both behaved the same.

    这篇关于我可以从命令行为Grails项目运行单个单元测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆