Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run a single unit test from the command line for a Grails project?

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.

like image 850
Eric Wilson Avatar asked Sep 09 '10 20:09

Eric Wilson


People also ask

How do I run unit test from console?

run Visual Studio unit tests by using MSTest.exe, located at %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe in my case. using /testcontainer:Path\To\Your\TestProjectAssembly. dll to indicate where your tests are coded. You can specify multiple '/testcontainer' options if required.

How do I run a single test in Xcode?

In Xcode 5 you can run a single test case by clicking the little play button that you can find next to the test in the test navigator or next to the test method in the editor. In both places it shows up when you hover over it.

How do I run a specific unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.


2 Answers

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.

like image 167
Rob Hruska Avatar answered Oct 14 '22 07:10

Rob Hruska


yes there is

grails test-app -unit YourController.testSomething 

where YourController is your controller and testSomething is the test method.

You should see something like

Tests PASSED - view reports in

like image 27
hvgotcodes Avatar answered Oct 14 '22 09:10

hvgotcodes