Details

    • Type: Sub-task Sub-task
    • Status: 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.

        Issue Links

          Activity

          Hide
          Chetan Mehrotra added a comment -

          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 - 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
          Chetan Mehrotra added a comment -

          Modified TestApp script to execute individual test methods

          Show
          Chetan Mehrotra added a comment - Modified TestApp script to execute individual test methods
          Hide
          Peter Ledbrook added a comment -

          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 - 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
          Chetan Mehrotra added a comment -

          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 - 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
          Peter Ledbrook added a comment -

          Implemented in such a way that you can run:

          grails test-app MyService.testSomeMethod
          
          Show
          Peter Ledbrook added a comment - Implemented in such a way that you can run: grails test-app MyService.testSomeMethod
          Hide
          Mark Berger added a comment -

          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 - 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
          boojapathy added a comment -

          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 - 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
          Luke Daley added a comment -

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

          Show
          Luke Daley added a comment - @boojapathy & @Mark Berger: Yes, this has been fixed for 1.3.2

            People

            • Assignee:
              Peter Ledbrook
              Reporter:
              Chetan Mehrotra
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development