Test your application

Source: Internet
Author: User
Tags mail account

Creating Automatic test Suites for your application is a good the IT robust. It allows a very agile.

Play tests is built using JUnit 4 or Selenium depending what do you want to test.

Writing Tests

The tests must is created in the test/ directory. This folder would have only been added to the sources path when the application was run in Test mode. You can write 3 different kinds of tests.

Unit Test

A unit test is written using JUnit. In this kind of test can test the model of your application (including some utilities).

Here's an example of a Unit test:

import play.test.*;import org.junit.*; public class MyTest extends UnitTest {         @Test    public void aTest() {        assertEquals(2, 1 + 1); // A really important thing to test    }     @Test    public void testUsers() {        assertEquals(3, Users.count());     } }
Functional test

A functional test is written using JUnit. In this kind of test can test your application by accessing directly the controller objects.

Here's an example of a functional test:

import play.test.*;import play.mvc.*;import play.mvc.Http.*;import org.junit.*; public class ApplicationTest extends FunctionalTest {         @Test    public void testTheHomePage() {        Response response = GET("/");        assertStatus(200, response);    }     }
Selenium Test

Acceptance tests is written using Selenium. Here's can test your application by running it in an automated browser.

Selenium tests is written using HTML tables. You can either use this native syntax or use the #{selenium/} tag.

Here's an example of a Selenium test:

#{selenium ‘Test security‘}     // Try to log in the administration area    clearSession()    open(‘/admin‘)    assertTextPresent(‘Login‘)    type(‘login‘, ‘admin‘)    type(‘password‘, ‘secret‘)    clickAndWait(‘signin‘)     // Verify that the user in correctly logged in    assertText(‘success‘, ‘Welcom admin!‘) #{/selenium}

Because Selenium tests is run within your browser access to emails sent by the mock e-mail and to String values placed in The Play Cache must is made using extensions to Selenium.

Here are an example of accessing the most recent emails sent to a specific e-mail account:

#{selenium ‘Test email sending‘}     // Open email form and send an email to [email protected]    open(‘/sendEmail‘)    assertTextPresent(‘Email form‘)    type(‘To‘, ‘[email protected]‘)    type(‘Subject‘, ‘Berillium Subject‘)    clickAndWait(‘send‘) // Extract the last email sent to [email protected] into a javascript variable called emailstoreLastReceivedEmailBy(‘bo[email protected]‘, ‘email‘)// Extract the subject line from the email variable into a variable called subjectstore(‘javascript{/Subject:\s+(.*)/.exec(storedVars["email"])[1]}‘, ‘subject‘)// Test the contents of the subject variableassertEquals(‘Berillium Subject‘, ‘$[subject]‘) #{/selenium}

Here's an example of accessing a String stored on the Play Cache (for example the correct answer to a CAPTCHA):

#{selenium ‘Get string from cache‘} open(‘/register‘)assertTextPresent(‘Registration form‘)type(‘Email‘, ‘[email protected]‘)type(‘Password‘, ‘secretpass‘)type(‘Password2‘, ‘secretpass‘)// .. Fill in the registration form ..

Get the value of the Magickey variable from the cache (set to the CAPTCHA answer in the application)
Storecacheentry (' Magickey ', ' captchaanswer ')
Type it into the form
Type (' Answer ', ' $[captchaanswer] ')

Clickandwait (' register ')

#{/selenium}

Fixtures

When you run the tests, you need to the stable data for your application. The simplest-to-reset your database before each test.

The play.test.Fixtures class helps you to manipulate your database and to inject test data. You typically use it in a @Before method of a JUnit test.

@Beforepublic void setUp() {    Fixtures.deleteAll();}

To import data, it's simpler to define them in a YAML file, the fixtures helper can automatically import.

# Test data Company(google):   name:    Google Company(zen):   name:    Zenexity User(guillaume):   name:    guillaume   company: zen

and then:

@Beforepublic void setUp() {    Fixtures.deleteAll();    Fixtures.loadModels("data.yml");}

You can read more on the Play and Yaml in the YAML manual page.

For Selenium tests, your can use the #{fixture/} tag:

#{fixture delete:‘all‘, load:‘data.yml‘ /} #{selenium}        // Write your test here #{/selenium}

Sometimes it is convenient-to-split data into several YAML files. You can load fixtures from multiple files at once:

Fixtures.loadModels("users.yml", "roles.yml", "permissions.yml");

And for Selenium tests:

#{fixture delete:‘all‘, load:[‘users.yml‘, ‘roles.yml‘, ‘permissions.yml‘] /}
Running the tests

To run the tests, you must run your application in test mode using the play Test command.

# play test myApp

In this mode, Play would automatically load the test-runner module. This module provides a Web based Test runner, available at the Http://localhost:9000/@tests URL.

When you run a test, the result was saved into the /test-result directory of your application.

On the Test Runner page, all test is a link. You can "right click" and "Open in a new tab", to run the test directly outside of the Test-runner.

When you run the tests this, Play would start with a special test framework ID. So you can define special configurations in the application.conf file.

If you want several different test-configuration, you can use the framework IDs matching the pattern ' test-?. * ' (e.g: ' test-special ').
IF you use a framework ID and then the default ' Test ', you must make sure all test configuration in application.conf are Available
With that framework ID. When the launching test with special test framework ID for you does it like this: ' Play Test--%test-your-special-id '

For example:

%test.db=mem%test.jpa.ddl=create-drop
Continuous integration, and running the tests automatically

The auto-test command does the same as the Test command, but it automatically launches a browser, runs a ll the tests, and stops.

A useful command if you want to set up a continuous integration system;

After the run, all results is saved to the /test-result directory. Moreover, this directory contains a marker file (either result.failed or result.passed) for the test sui Te ' s final result. Finally, this directory is contains all the logs, with an application.log file.

So setting-a continuous Integration system to test your application, could is:

    • Checkout the latest version of your application
    • Run Play Auto-test
    • Wait for the end of the process
    • Check for the marker file result.passed or result.failed in the /test-result directory

Run these steps in a CRON tab, and you ' re done!

Continuing the discussion

Next: Security Guide.

Test your application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.