我喜欢用于 JavaScript 单元测试的 qUnit,并且已经成功地将它用于几乎完全是 AJAX 的大型网络托管平台。但是,我必须在浏览器中手动运行它,或者作为 Windows 计划任务运行,这并不理想。
有没有人运行 jUnit 测试作为自动化测试套件的一部分,就像你在(比如说)perl 或 Java 中那样?
我喜欢用于 JavaScript 单元测试的 qUnit,并且已经成功地将它用于几乎完全是 AJAX 的大型网络托管平台。但是,我必须在浏览器中手动运行它,或者作为 Windows 计划任务运行,这并不理想。
有没有人运行 jUnit 测试作为自动化测试套件的一部分,就像你在(比如说)perl 或 Java 中那样?
最简单的方法是从 JUnit 测试中使用Selenium 2运行 qUnit测试。Selenium 2 在 Firefox、IE、Chrome 或它自己的 HtmlDriver 中打开网页,并且几乎可以用渲染页面完成所有操作,尤其是 qUnit 测试结果。
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"));
}
}
我会推荐jstestdriver。它允许您从命令行对浏览器的真实实例运行测试,这意味着它可以在 CI 构建中使用,也可以简单地作为构建脚本的一部分运行。
它有自己的断言框架,我发现它比 qUnit 更好。但是,如果由于某种原因需要 qUnit,那么有一个插件允许您为 jstestdriver 运行器编写 qUnit 测试。