在PhantomJS中从Ant运行异步QUnit测试 [英] Running asynchronous QUnit tests from Ant in PhantomJS

查看:85
本文介绍了在PhantomJS中从Ant运行异步QUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PhantomJS 从Ant构建脚本运行一组异步QUnit测试。我有什么似乎工作,但似乎应该有一个更好的方法来实现它。

I am attempting to get a set of asynchronous QUnit tests to run from an Ant build script, using PhantomJS. What I have seems to be working, but it seems like there should be a nicer way to achieve it.

PhantomJS加载时运行的脚本(简化)如下:

The script (simplified) that runs when PhantomJS loads is as follows:

var page = require("webpage").create();

page.onConsoleMessage = function(msg) {
    if(msg.indexOf("FINISHED") > -1) {
        var failed = msg.split(" ");
        phantom.exit(failed[1]);
    }
};

page.open("testrunner.html", function() {
    page.evaluate(function() {
        QUnit.done = function(result) {
            console.log("FINISHED " + result.failed);
        };
    });
});

这会加载包含测试的文件( testrunner.html )。它使用PhantomJS evaluate 方法在加载页面的上下文中运行一些代码。该代码将事件处理程序绑定到QUnit done 事件。在事件处理程序中,所有发生的事情都是对 console.log 的简单调用。

This loads the file which contains the tests (testrunner.html). It uses the PhantomJS evaluate method to run some code in the context of the loaded page. That code binds an event handler to the QUnit done event. In the event handler, all that happens is a simple call to console.log.

PhantomJS什么都不做默认情况下使用 console.log 调用,因此我还将事件处理程序绑定到PhantomJS onConsoleMessage 事件。当 console.log 调用 QUnit.done 事件处理程序时, onConsoleMessage 触发事件。如果控制台消息与给定字符串匹配,那么我们就知道测试已经完成运行。然后我们可以退出PhantomJS,退出代码等于失败的单元测试的数量(Ant脚本使用它来确定构建的这部分是否成功)。

PhantomJS does not do anything with console.log calls by default, so I have also bound an event handler to the PhantomJS onConsoleMessage event. When the console.log call in the QUnit.done event handler is executed, the onConsoleMessage event is triggered. If the console message matches a given string, then we know the tests have finished running. We can then exit PhantomJS, with an exit code equal to the number of failed unit tests (this is used by the Ant script to determine whether or not this part of the build was successful).

我的问题是,有没有更好的方法来确定单元测试何时完成运行?

My question is, is there a better way to find out when the unit tests have finished running?

推荐答案

你有没有看过幻影js网站上提供的qunit跑步者? https://github.com/ariya/phantomjs/blob/1.2 /examples/run-qunit.js

Have you checked out the qunit runner that is provided on the phantom js webiste? https://github.com/ariya/phantomjs/blob/1.2/examples/run-qunit.js

基本上你只需将它指向你的qunit测试页面(testrunner.html)并抓取页面以查看是否任何事情都失败了,并将结果输出到控制台。您可以修改它以打印到一个文件,然后将其集成到您的构建脚本中。

Essentially you just point it to your qunit test page (testrunner.html) and it scrapes the page to see if anything has failed and outputs the results to the console. You could modify it to print to a file which can then be integrated into your build script.

这篇关于在PhantomJS中从Ant运行异步QUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆