Japanese Functional Testing

Last updated by admin 3 years ago

??????????? {excerpt:hidden=true}Functional Testing{excerpt}

{excerpt:hidden=true} In addition to unit testing, Grails supports functional testing of your web application.{excerpt}

???????????Grails?????????????????????????????????{excerpt:hidden=true} For this purpose, it uses the free open-source Canoo WebTest.{excerpt} ????????????????? Canoo WebTest???????

??????????????????? {excerpt:hidden=true}How to test your web application{excerpt}

????????? {excerpt:hidden=true}Install and create{excerpt}

{excerpt:hidden=true}From the root directory of your application call{excerpt} ????????????????????????????????

grails create-webtest
{excerpt:hidden=true} This results in the following actions{excerpt} ?????????? {excerpt:hidden=true}
  • downloads Canoo WebTest if needed and installs under {{grails.home/downloads/webtest}}. The intial dowload can take a few minutes. The progress of download is indicated.
  • creates the test directory structure for the current application
  • creates standard properties and a standard test suite{excerpt}
  • ????Canoo WebTest??????????{{grails.home/downloads/webtest}} ??????????????????????????????????????????????
  • ?????????????test??????????????
  • ?????????test suite??????
{excerpt:hidden=true} To scaffold a webtest for a given domain class use{excerpt} ?????????????????webtest????????????????
grails generate-webtest
{excerpt:hidden=true} and enter the name of the domain class when prompted.{excerpt} ??????????????????????????????? {excerpt:hidden=true}
  • creates an initial webtest for the default views and controllers of the domain class
  • tests basic operations: list, create, delete through the web GUI
{excerpt}
  • ?????????????????????????????webtest???
  • web GUI?????????????: list, create, delete ????

?????? {excerpt:hidden=true}Running the tests{excerpt}

{excerpt:hidden=true} From the root directory of your application call{excerpt} ????????????????????????????

grails run-webtest
{excerpt:hidden=true} Runs the tests and puts reports in the {{reports}} dir.{excerpt} ?????????{{reports}} ?????????????????? {excerpt:hidden=true} When on Windows, the browser is automatically opened on the HTML report.{excerpt} Window?????????????????HTML????????????? {excerpt:hidden=true} It will present you a header with summarized and statistical data like below.{excerpt} ???????????????????????????????? {excerpt:hidden=true} A trailing section shows details about all operations effected during the test.{excerpt} ?????????????????????????????????????????? {excerpt:hidden=true} Note that WebTest stores all result pages it received from the server. From the test report, you can click on the corresponding link to find out what page the test engine worked upon at this point.{excerpt} ??????WebTest??????????????????????????? ????????????????????????????????????????? ???????????? {tip:title=Test First} {excerpt:hidden=true} WebTests can even be started if the server is not running. You can actually run the test before you have coded anything.{excerpt} WebTest????????????????????????????????????????????????????????????? {excerpt:hidden=true} In this case the report will show a lot of failed tests but that's helpful anyways. You'll see what behavior the tests expect and how the report is generated.{excerpt} ?????????????????????????????????????????????????????????????????????????????????????????????????? {tip}

?????? {excerpt:hidden=true}Defining your tests{excerpt}

Webtest??????????? {excerpt:hidden=true}The webtest folder layout{excerpt}

{excerpt:hidden=true} After creation and generation of a test for the {{mydomain}} domain class, you'll find the following files and folders below your application folder:{excerpt} ????????(grails create-webtest)? {{mydomain}} ????????????????(grails generate-webtest)?????????????????????????????????????????????:

myapp
+-- webtest
    +-- conf    (webtest.properties)
    +-- reports (readme.txt)
    +-- tests   (TestSuite.groovy, MydomainTest.groovy)
{excerpt:hidden=true} Notable places for costomization are || folder || file || customize for || | conf | webtest.properties | defining host/port/app for testing other than default | | tests | TestSuite.groovy | manually defining the suite if auto-detection is not wanted | | tests | MydomainTest.groovy | specifying the test steps (see below) | {excerpt} ???????????????? || ???? || ???? || ?????????? || | conf | webtest.properties | ????????????????????host/port/app | | tests | TestSuite.groovy | auto-detection??????????suite????????? | | tests | MydomainTest.groovy | ????????(?????) |

?????????? {excerpt:hidden=true}Adapting the test logic{excerpt}

{excerpt:hidden=true} MydomainTest.groovy will initially contain lines like{excerpt} MydomainTest.groovy????????????????????

class MydomainTest extends grails.util.WebTest {

// ????????????????????????????????????? // ??????????????? void suite() { testMydomainListNewDelete() // ??????????????? }

def testMydomainListNewDelete() { webtest('Mydomain basic operations: view list, create new entry, back to view, delete, view'){ invoke(url:'mydomain') verifyText(text:'Home')

verifyListPage(0) // <- ???????????????????????

clickLink(label:'New Mydomain') verifyText(text:'Create Mydomain') clickButton(label:'Create')

// more … }

{excerpt:hidden=true} There are multiple steps like {{invoke}}, {{clickButton}}, {{verifyXXX}}, etc. that make up the test specification.{excerpt} ??????????????{{invoke}}, {{clickButton}}, {{verifyXXX}} ????????????????????{excerpt:hidden=true} Find the full list of available steps, their parameters, a comprehensive description, and examples under WebTest Docs. {excerpt} WebTest Docs??????????????????????????????????????????? {excerpt:hidden=true} The online documentation lists these steps in their ANT notation (i.e. XML). This exactly maps to the builder calls inside the {{webtest()}} methods in MyappTest.groovy. Behind the scenes, there is an AntBuilder that cares for the mapping, effectively producing a Groovy-API on top of Canoo WebTest.{excerpt} ??????????????ANT???????(???XML)??????????????????????????????????MyappTest.groovy? {{webtest()}} ??????????????????????Canoo WebTest????????Groovy-API???????????????AntBuilder??????? {excerpt:hidden=true} Adapt the lines in MydomainTest.groovy to match the expected behavior of your webapp. If you have any bootstrapped data, your tests can rely on this data being available when starting the tests. {excerpt} webapp??????????????????MydomainTest.groovy????????????????????????????????????????????????????????????????????

??????? {excerpt:hidden=true}More Tests{excerpt}

{excerpt:hidden=true} You can have many tests like MydomainTest.groovy in the {{tests}} directory or its subdirectories.{excerpt} {{tests}} ?????????????????????MydomainTest.groovy????????????????{excerpt:hidden=true} Every such test must extend {{grails.util.WebTest}} and provide a {{suite}} method that calls each available test method. In subdirectories, tests must have the according package statements. {excerpt} ??????????????{{grails.util.WebTest}} ?extend????????????????????? {{suite}} ?????????????????? {excerpt:hidden=true} Tests will automatically be picked up by TestSuite.groovy. You can customize this logic to define your own suite (see its inlined comments on how to do that). {excerpt} ????TestSuite.groovy??????????????????????suite??????????????????????????????(?????????????????)? {excerpt:hidden=true} Customization of the suite is sometimes needed to assert a special sequence of test execution or to restrict the test execution to a subset of availabable tests. {excerpt} suite???????????????????????????????????????????????????????????????????

    null null null null null
  1. testing
  2. nullnullnullnullnull
_Dierk Koenig_