Grails JIRA

  • Log In Access more options
    • Online Help
    • GreenHopper Help
    • Agile Answers
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • Agile
Grails
  • Grails
  • GRAILS-787 Top level task: Improve Grails unit t...
  • GRAILS-2473

Running a single test method from test suite

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0
  • Fix Version/s: 1.1-RC1
  • Component/s: Project infrastructure
  • Labels:
    None

Description

Current TestApp script executes the whole TestSuite/TestCase. many a times we would only want to run a particular test method. So it would be helpful if we can run individual test method like

>grails test-app TestClass.testMethod

The above command should only execute the testMethod and not the entire testcase of TestClass.

  • Options
    • Sort By Name
    • Sort By Date
    • Ascending
    • Descending
    • Download All

Attachments

  1. File
    TestApp2.groovy
    17/Feb/08 12:33 PM
    17 kB
    Chetan Mehrotra

Issue Links

relates to

Bug - A problem which impairs or prevents the functions of the product. GRAILS-2293 integration test total reported incorrectly

  • Minor - Minor loss of function, or other problem where easy workaround is present.
  • Closed - The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
  • Git Commits
Hide
Permalink
Chetan Mehrotra added a comment - 17/Feb/08 12:32 PM

To accomplish this I have modified the TestApp script from Grails distribution. It is attached as TestApp2.groovy. The approach taken by me is sort of hacky and not that clean. To execute the individual testMethods I added

class GrailsMethodTestSuite extends GrailsTestSuite {
private def methodNames
private def clazz
public GrailsMethodTestSuite(applicationContext,clazz,methods)

{ super(applicationContext,clazz) this.methodNames = methods this.clazz = clazz addTestMethods() }

public void addTest(Test test)

{ //eat up the test methods which are added during initialization in //TestSuite constructor }

def addTestMethods(){
TestSuite ts = new TestSuite(clazz) //create a testsuite so as to get testnames
for (Enumeration e= ts.tests(); e.hasMoreElements(); ) {
def test= e.nextElement();
if(this.methodNames.contains(test.name))

{ super.addTest(test) }

}
}
}

In the script only and modified few parts so as to use GrailsMethodTestSuite if any test method is specified . By default the script uses GrailsTestSuite if no method is specified. The above suite would eat up any test methods added during testsuite initialization and later adds the one we are interested in.

Show
Chetan Mehrotra added a comment - 17/Feb/08 12:32 PM To accomplish this I have modified the TestApp script from Grails distribution. It is attached as TestApp2.groovy. The approach taken by me is sort of hacky and not that clean. To execute the individual testMethods I added class GrailsMethodTestSuite extends GrailsTestSuite { private def methodNames private def clazz public GrailsMethodTestSuite(applicationContext,clazz,methods) { super(applicationContext,clazz) this.methodNames = methods this.clazz = clazz addTestMethods() } public void addTest(Test test) { //eat up the test methods which are added during initialization in //TestSuite constructor } def addTestMethods(){ TestSuite ts = new TestSuite(clazz) //create a testsuite so as to get testnames for (Enumeration e= ts.tests(); e.hasMoreElements(); ) { def test= e.nextElement(); if(this.methodNames.contains(test.name)) { super.addTest(test) } } } } In the script only and modified few parts so as to use GrailsMethodTestSuite if any test method is specified . By default the script uses GrailsTestSuite if no method is specified. The above suite would eat up any test methods added during testsuite initialization and later adds the one we are interested in.
Hide
Permalink
Chetan Mehrotra added a comment - 17/Feb/08 12:33 PM

Modified TestApp script to execute individual test methods

Show
Chetan Mehrotra added a comment - 17/Feb/08 12:33 PM Modified TestApp script to execute individual test methods
Hide
Permalink
Peter Ledbrook added a comment - 17/Feb/08 2:41 PM

First, thanks for the patch!

Second, the following code in TestApp:

            // If the test name includes a package, replace it with the
            // corresponding file path.
            if (it.indexOf('.') != -1) {
                 if(containsMethodName(it)){//filter out te method name
                     it = it.substring(0,it.lastIndexOf('.'))
                 }
                 it = it.replace('.' as char, '/' as char)
            }
            else {
                // Allow the test class to be in any package.
                it = "**/$it"
            }

            return "${it}Tests"

should probably be:

            if (containsMethodName(it)) {//filter out te method name
                it = it.substring(0,it.lastIndexOf('.'))
            }

            // If the test name includes a package, replace it with the
            // corresponding file path.
            if (it.indexOf('.') != -1) {
                 it = it.replace('.' as char, '/' as char)
            }
            else {
                // Allow the test class to be in any package.
                it = "**/$it"
            }

            return "${it}Tests"

In other words, you should filter out the method name before checking for a package.

Finally, it would probably be best to modify GrailsTestSuite to use the default TestSuite constructor when method names are supplied, and then manually add the tests using 'addTest(TestSuite.createTest(clazz, methodName)'.

In fact, it might be a good idea to have GrailsTestSuite handle the <class>.<method> form directly.

Show
Peter Ledbrook added a comment - 17/Feb/08 2:41 PM First, thanks for the patch! Second, the following code in TestApp: // If the test name includes a package , replace it with the // corresponding file path. if (it.indexOf('.') != -1) { if (containsMethodName(it)){ //filter out te method name it = it.substring(0,it.lastIndexOf('.')) } it = it.replace('.' as char , '/' as char ) } else { // Allow the test class to be in any package . it = "**/$it" } return "${it}Tests" should probably be: if (containsMethodName(it)) { //filter out te method name it = it.substring(0,it.lastIndexOf('.')) } // If the test name includes a package , replace it with the // corresponding file path. if (it.indexOf('.') != -1) { it = it.replace('.' as char , '/' as char ) } else { // Allow the test class to be in any package . it = "**/$it" } return "${it}Tests" In other words, you should filter out the method name before checking for a package. Finally, it would probably be best to modify GrailsTestSuite to use the default TestSuite constructor when method names are supplied, and then manually add the tests using 'addTest(TestSuite.createTest(clazz, methodName)'. In fact, it might be a good idea to have GrailsTestSuite handle the <class>.<method> form directly.
Hide
Permalink
Chetan Mehrotra added a comment - 17/Feb/08 2:57 PM

Hi Peter

Filtering out method name is more cleaner. I gone this way as it did not required creating any java class so sort of quick and dirty way. Ideally GrailsTestSuite should handle the test methods directly.

Show
Chetan Mehrotra added a comment - 17/Feb/08 2:57 PM Hi Peter Filtering out method name is more cleaner. I gone this way as it did not required creating any java class so sort of quick and dirty way. Ideally GrailsTestSuite should handle the test methods directly.
Hide
Permalink
Peter Ledbrook added a comment - 18/Feb/09 12:06 PM

Implemented in such a way that you can run:

grails test-app MyService.testSomeMethod
Show
Peter Ledbrook added a comment - 18/Feb/09 12:06 PM Implemented in such a way that you can run: grails test-app MyService.testSomeMethod
Hide
Permalink
Mark Berger added a comment - 26/May/10 6:34 PM

This doesn't seem to work in 1.3.1, but does in 1.2.2.

Create simple grails app, add a domain class nz.Foo, edit FooTests, add two methods testA, testB.

Then running grails test-app nz.testA runs both testA and TestB.

Doing the same steps under 1.2.2, only runs testA.

Is this a known issue?

Show
Mark Berger added a comment - 26/May/10 6:34 PM This doesn't seem to work in 1.3.1, but does in 1.2.2. Create simple grails app, add a domain class nz.Foo, edit FooTests, add two methods testA, testB. Then running grails test-app nz.testA runs both testA and TestB. Doing the same steps under 1.2.2, only runs testA. Is this a known issue?
Hide
Permalink
boojapathy added a comment - 08/Jun/10 2:46 AM

In grails 1.3 we are also facing the issue where a single integration test could not be run. It runs the entire test file.

Show
boojapathy added a comment - 08/Jun/10 2:46 AM In grails 1.3 we are also facing the issue where a single integration test could not be run. It runs the entire test file.
Hide
Permalink
Luke Daley added a comment - 08/Jun/10 2:58 AM

@boojapathy & @Mark Berger: Yes, this has been fixed for 1.3.2

Show
Luke Daley added a comment - 08/Jun/10 2:58 AM @boojapathy & @Mark Berger: Yes, this has been fixed for 1.3.2

People

  • Assignee:
    Peter Ledbrook
    Reporter:
    Chetan Mehrotra
Vote (1)
Watch (1)

Dates

  • Created:
    17/Feb/08 12:25 PM
    Updated:
    18/May/11 3:17 PM
    Resolved:
    18/Feb/09 12:06 PM

Agile

  • View on Board
  • Atlassian JIRA (v5.2.1#813-sha1:277a546)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Grails project. Try JIRA - bug tracking software for your team.