简体   繁体   English

使用qUnit进行Javascript测试

[英]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. 我喜欢用于JavaScript单元测试的qUnit,并且已经成功地将它用于一个几乎完全是AJAX的大型Web托管平台。 However, I have to run it in a browser by hand, or as a Windows scheduled task, which is not ideal. 但是,我必须手动在浏览器中运行它,或者作为Windows计划任务,这是不理想的。

Has anyone run jUnit tests as part of an automated test suite, like you would in (say) perl or Java? 有没有人将jUnit测试作为自动测试套件的一部分运行,就像你在perl或Java中那样?

The simplest way could be running qUnit test with Selenium 2 , from JUnit test. 最简单的方法是使用Selenium 2从JUnit测试运行qUnit测试。 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. 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"));     
}   
}

I would recommend jstestdriver . 我会推荐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. 它允许您对命令行的实际实例运行测试,这意味着它可以在CI构建中使用,或者只是作为构建脚本的一部分运行。

It has it's own assertion framework, which I have found to be better than qUnit. 它有自己的断言框架,我发现它比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. 但是,如果由于某种原因需要qUnit,那么有一个插件允许您为jstestdriver运行器编写qUnit测试。

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

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