如何在 JavaScript 中运行(类似 Python)文档测试?
是否有 JavaScript 测试框架提供与 Python 的 doctest 大致相当的功能?
function add(a, b) {
/**
Returns the sum of `a` and `b`:
> add(1, 3)
4
Add coerces types to numeric values where possible:
> add('51' + 3)
54
*/
return (a - 0) + (b - 0);
}
Do any JavaScript test frameworks provide a rough equivalent to Python's doctest?
function add(a, b) {
/**
Returns the sum of `a` and `b`:
> add(1, 3)
4
Add coerces types to numeric values where possible:
> add('51' + 3)
54
*/
return (a - 0) + (b - 0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法理解 Ian Bicking 的包 doctestjs 的意义。他只是为我们提供了一种编写正常外部测试的不同方式,而不是真正的文档测试。
我使用了很多 python 文档测试,它们对我来说非常重要,我不知道 doctestjs 有什么用处,但我发现了这个项目实现的一些真正的文档测试: https://github.com/davidchambers/doctest 。
即使这不是最流行的 javascript 文档测试项目,我也非常喜欢它。
更新:在回答这个问题一年多后,我有机会为 David Chambers 的项目做出贡献,它写得非常好。我还在使用 require.js 的复杂应用程序中使用了它,为此我们添加了对 AMD 模块的支持。我认为他还添加了对 Common JS 模块的支持。所以我只能确认我的观点。
I can't get the point of Ian Bicking's package, doctestjs. He just provides us a different way of writing normal external tests, not real doctests.
I use a lot python doctests, they are quite important for me, I don't know what doctestjs could be useful for, but I found some true doctests implemented with this project: https://github.com/davidchambers/doctest .
Even if that is not the most trendy doctest project for javascript, I strongly prefer it.
Update: after more that one year since this answer, i had the opportunity to contribute to the project from David Chambers, it is really well written. I also used it at work in a complex application using require.js, and for this we added support for AMD modules. I think he added support for Common JS modules as well. Thus i can only confirm my opinion.
不知道昨天抽的是什么烟。苏里。
doctestjs (doctestjs on github)是正确的链接,它似乎也在积极开发中。
尽管它的工作方式与 python doctest 略有不同,因为使用 doctestjs 您可以在 HTML 文件中声明测试,以便可以运行它们。
但我想应该可以在代码中声明内联测试。然后在构建过程中执行预处理步骤以提取测试并自动从中创建测试 html,
例如当使用ant时,我想象一个应用复制文件,replaceregexp,concat。
例如,复制 js 文件,替换所有不是注释的内容以及所有看起来不像 doctestjs 测试的注释。然后 concat htmlhead+tests+headfooter 完成。
Don't know what I was smoking yesterday. Soory.
doctestjs (doctestjs on github) is the correct link and it seems to be under active development too.
Although it works a little different from the python doctest in that with doctestjs you declare the tests in a HTML file so they can be run.
But I guess it should be possible to declare the test inline in your code. And then do a preprocessing step in your build process to extract the tests and automagically create a test-html from them
e.g. when using ant I imagine an applying copy file, replaceregexp, concat.
e.g. copy the js file, replace everything which isn't a comment and all comments which don't look liks doctestjs tests. then concat htmlhead+tests+headfooter done.