36

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.

Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
  • 1
    can you post the actual command line you are entering, and the output? As you can see from the answers, this should work so there is a problem somewhere before the actual typing of the command. For example what is the name of the test? what is the name of the method in the testcase? – Aaron Saunders Sep 17 '10 at 15:02
  • What version are you on? I believe this feature was introduced in 1.2 release. http://www.grails.org/1.2+Release+Notes – Deepak Mittal Sep 10 '10 at 06:13
  • The first comment is misleading, you can test single Unit tests with Grails 1.1.1 – seba.wagner Jul 04 '13 at 04:14

5 Answers5

44

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.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
13

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

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
9

Given a test class foo.BarTests you can run just the tests in that class using the following command:

grails test-app :unit foo.Bar

Or run a single test method within that class using:

grails test-app :unit foo.Bar.testMethod

Note that you do not include the word "Tests" when specifying the name of test class.

John Wagenleitner
  • 10,967
  • 1
  • 40
  • 39
3
grails test-app -unit com.package.YourController.testSomething

you need to include the package name when you run the test

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • I guessed that, still didn't work. Does it make a difference if it is a service class, and not generated, like a controller? – Eric Wilson Sep 09 '10 at 22:09
3

This would work for sure, I use it every day

Following will run a single test

grails test-app :unit ExampleControllerTests.testName

Following will run all ExampleControllerTests

grails test-app :unit ExampleControllerTests
Sudhir N
  • 4,008
  • 1
  • 22
  • 32
  • The OP specifically said that using the name of the test class was his error, but here your examples show doing exactly that. Plus your syntax looks suspect, with a trailing colon instead of a leading dash, though I don't really know... – ErikE Feb 05 '12 at 09:20
  • 1
    Don't comment and vote negative if you don't really know, this works exact. Using the name of test class is no problem at all, I use it every day and i know it works. (I use grails 1.3.7) – Sudhir N Feb 05 '12 at 16:41
  • I don't know how to reconcile what you said with what the OP said was the definitive answer. The downvote was not for the syntax which by itself I would have ignored. – ErikE Feb 05 '12 at 19:45
  • 2
    Hmm, that works for me in Grails 2.0. I'm curious what version I was using 17 months ago when I was having this difficulty. @ErikE, I'd suggest that you reverse your vote, as this answer is correct, despite my previous assertions. – Eric Wilson Feb 06 '12 at 13:14
  • @Eric Thank you for the info! I commented in addition to downvoting to give a chance to fix the problem--if it existed. Now this has generated the chance for someone to update an answer or a question with the new information. I will never downvote unfairly if I can help it. Give some trust to the process, folks. :) – ErikE Feb 06 '12 at 17:18
  • This syntax works for running integration tests also, like.. `grails test-app integration: TestSpec` etc. – Ian Durkan Feb 26 '14 at 19:02