私はJavaScriptユニットテスト用のqUnitが大好きで、ほぼ独占的にAJAXである大規模なWebホスティングプラットフォームで正常に使用しています。ただし、ブラウザで手動で実行するか、Windowsのスケジュールされたタスクとして実行する必要があります。これは理想的ではありません。
perlやJavaのように、自動テストスイートの一部としてjUnitテストを実行した人はいますか?
私はJavaScriptユニットテスト用のqUnitが大好きで、ほぼ独占的にAJAXである大規模なWebホスティングプラットフォームで正常に使用しています。ただし、ブラウザで手動で実行するか、Windowsのスケジュールされたタスクとして実行する必要があります。これは理想的ではありません。
perlやJavaのように、自動テストスイートの一部としてjUnitテストを実行した人はいますか?
最も簡単な方法は、JUnitテストからSelenium2を使用してqUnitテストを実行することです。Selenium 2は、Firefox、IE、Chrome、または独自のHtmlDriverでWebページを開き、レンダリングされたページ、特に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テストを記述できるプラグインがあります。