我正在寻找一个可以在任何上下文中轻松使用的 JavaScript 测试框架,无论是浏览器、控制台、XUL 等。
是否有这样的框架,或者是否有一种方法可以轻松改造现有框架,使其与上下文无关?
编辑:测试框架应该不绑定(bind)到任何其他框架,例如 jQuery 或 Prototype.js,并且不应该依赖于存在的 DOM(或文档对象)。我正在寻找可以测试纯 JavaScript 的东西。
最佳答案
好的,这是我刚刚根据一些早期工作酿造的东西。我希望这能满足您的需求。
jsUnity
轻量级通用 JavaScript 测试框架
jsUnity is a lightweight universal JavaScript testing framework that is context-agnostic. It doesn't rely on any browser capabilities and therefore can be run inside HTML, ASP, WSH or any other context that uses JavaScript/JScript/ECMAScript.
HTML 中的示例用法
<pre>
<script type="text/javascript" src="../jsunity.js"></script>
<script type="text/javascript">
function sampleTestSuite() {
function setUp() {
jsUnity.log("set up");
}
function tearDown() {
jsUnity.log("tear down");
}
function testLessThan() {
assertTrue(1 < 2);
}
function testPi() {
assertEquals(Math.PI, 22 / 7);
}
}
// optionally wire the log function to write to the context
jsUnity.log = function (s) { document.write(s + "</br>"); };
var results = jsUnity.run(sampleTestSuite);
// if result is not false,
// access results.total, results.passed, results.failed
</script>
</pre>
上面的输出:
2 tests found set up tear down [PASSED] testLessThan set up tear down [FAILED] testPi: Actual value does not match what's expected: [expected] 3.141592653589793, [actual] 3.142857142857143 1 tests passed 1 tests failed
关于javascript - 与上下文无关的 JavaScript 测试框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/489150/