Hogsmill
Hogsmill

Reputation: 1574

Using qUnit for Javascript testing

I love qUnit for JavaScript unit testing, and have successfully used it for a large web hosting platform that is almost exclusively AJAX. However, I have to run it in a browser by hand, or as a Windows scheduled task, which is not ideal.

Has anyone run jUnit tests as part of an automated test suite, like you would in (say) perl or Java?

Upvotes: 8

Views: 3089

Answers (2)

rlayte
rlayte

Reputation: 538

I would recommend jstestdriver. It allows you to run tests against real instances of browsers but from the command line, which means it can be used in a CI build or simply run as part of your build script.

It has it's own assertion framework, which I have found to be better than qUnit. However, if qUnit is required for some reason then there is a plugin that allows you to write qUnit tests for the jstestdriver runner.

Upvotes: 5

jinowolski
jinowolski

Reputation: 2472

The simplest way could be running qUnit test with Selenium 2, from JUnit test. Selenium 2 opens webpages in Firefox, IE, Chrome or its own HtmlDriver and can do almost everything with a rendered page, especially with qUnit test results.

import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FooTest {

static WebDriver driver;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    driver = new FirefoxDriver();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
    driver.close();
}

@Test
public void bar() throws Exception {
    driver.get("http://location/of/qUnitTest");

    //Handling output could be as simple as checking if all 
    //test have passed or as compound as parsing all test results 
    //and generating report, that meets your needs.
    //Code below is just a simple clue.
    WebElement element = driver.findElement(By.id("blah"));
    assertFalse(element.getText().contains("test failed"));     
}   
}

Upvotes: 7

Related Questions