繁体   English   中英

使用qUnit进行Javascript测试

[英]Using qUnit for Javascript testing

我喜欢用于JavaScript单元测试的qUnit,并且已经成功地将它用于一个几乎完全是AJAX的大型Web托管平台。 但是,我必须手动在浏览器中运行它,或者作为Windows计划任务,这是不理想的。

有没有人将jUnit测试作为自动测试套件的一部分运行,就像你在perl或Java中那样?

最简单的方法是使用Selenium 2从JUnit测试运行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测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM