【问题标题】:Using qUnit for Javascript testing使用 qUnit 进行 Javascript 测试
【发布时间】:2011-05-27 00:55:26
【问题描述】:

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

是否有人将 jUnit 测试作为自动化测试套件的一部分运行,就像您在(例如)perl 或 Java 中那样?

【问题讨论】:

    标签: javascript unit-testing tdd qunit


    【解决方案1】:

    最简单的方法是使用来自 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"));     
    }   
    }
    

    【讨论】:

      【解决方案2】:

      我会推荐jstestdriver。它允许您从命令行对浏览器的真实实例运行测试,这意味着它可以在 CI 构建中使用,也可以作为构建脚本的一部分运行。

      它有自己的断言框架,我发现它比 qUnit 更好。但是,如果由于某种原因需要 qUnit,那么有一个插件可以让您为 jstestdriver 运行器编写 qUnit 测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 1970-01-01
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        相关资源
        最近更新 更多