测试 JavaScript 代码而不重新创建标记?
我想在我们的网络应用程序中测试 JavaScript 代码。 我们正在使用 jQuery 库和 .Net MVC。 我查看了这些答案和jqUnit 看起来不错,所以我尝试了它,只是为了意识到我必须为我想测试的每个页面重新创建所有标记。
我错过了什么吗? 是否有其他测试 JS/jQuery 代码的方法? 我们的标记有时很复杂,并且会因 AJAX 调用而发生巨大变化。
我能想到的最好办法就是尝试将我们的应用程序粘贴到测试页面的 iframe 中。 还没试过。
I want to test the JavaScript code in our web app. We are using the jQuery library and .Net MVC. I've looked at these answers, and jqUnit looked nice, so I tried it, just to realize that I'll have to pretty much recreate all markup for every page I want to test.
Am I missing something? Are there any alternative approaches for testing JS/jQuery code? Our markup is at times complex, and changes drastically as a result of AJAX calls.
The best I can think of is trying to stick our app in an iframe on a test page. Haven't tried it yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您测试 javascript 函数/对象,我可以建议 YUI 的测试组件。
http://developer.yahoo.com/yui/yuitest/
与 JUnit 非常相似的设置但只需要您包含一些测试 javascript 文件。 仅在测试模式下将其包含在您的页面中将非常容易。
If your testing javascript functions/objects, might i suggest YUI's testing component.
http://developer.yahoo.com/yui/yuitest/
Very similar setup to JUnit but only requires that you include a few test javascript files. It would be pretty easy to include this in your page while in a Test mode only.
您可以只在内存中创建要测试的结构,而不将其附加到文档中。 这不适用于所有测试(例如 CSS 属性不会影响未插入到文档中的项目,但在某些情况下这可能值得一试)。
首先,它非常快,其次,您不会因为完成测试后需要删除的测试项目而破坏您的文档。
这是一个非常简单的例子中的想法。
You could just create the structure you want to test in the memory without appending it to the document. This won't work will all the tests (as for example CSS propeties won't affect items not inserted to the document but in some cases this may be worth a try).
First it's very fast, and second you don't spoil your document with test items you need to remove after you complete the tests.
Here's that idea in a very simple example.
您可能需要两种方法,具体取决于代码:
您可能想弄清楚如何重组代码,以便可以通过这种方式进行测试。 你能让 JS 更加模块化,或者更少地依赖于某种标记吗? (遗憾的是,有时您不能。)
它们的缺点是运行速度往往较慢,并且维护起来有点困难或脆弱。 查找“页面对象”模式来帮助您解决可维护性方面的问题。
我们最终使用 selenium 来驱动 JS 单元测试的自动化。
You may need two approaches, depending on the code:
You may want to figure out how you can restructure your code such that it can be tested in this way. Can you make the JS more modular, or less dependent on a certain markup? (Sadly, sometimes you can't.)
The disadvantage of these is that they tend to be slower to run and a little harder to maintain or brittle. Look up the "Page Object" pattern to help you out on the maintainability front.
We end up automating the JS unit tests using selenium to drive it.
可能值得考虑 Selenium 并在浏览器级别运行一些自动化测试。
当然,在理想的情况下,您会在代码级别使用单元测试框架,但考虑到这可能涉及大量的前期返工,一个可靠的折衷方案可能是测试 javascript 是否确实满足您的需要使用 Selenium 或类似的框架。
It might be worth considering Selenium and running some automated tests at the browser level.
Of course, in an ideal world you would work with a unit-testing framework at the code-level, but given that may involve a lot of up-front rework, a solid compromise might be to test that the javascript actually does what you need it to using Selenium or a similar framework.
在 iframe 中测试您的应用程序是可行的。
我发现更好的是坚持使用模型-视图-呈现器 (MVP) 范例(仅在客户端),同时将 UI 组件分解为可消化的块,以便能够在其自己的“驱动程序”内测试每个组件“ 页。 大多数逻辑驻留在 Presenter 内部,它是纯 JavaScript,因此可以使用纯 JavaScript 单元测试进行测试。 单个 UI 组件可以使用像 Selenium 这样的框架进行测试,并且它们的测试只是确保它们向 Presenter 抛出正确的事件。 如果后端访问部分被存根/模拟到某种 Fetcher 类中,则模型也可以作为纯 JavaScript 进行测试。
Testing your app inside an iframe could work.
What I've found to be better is to stick with the Model-View-Presenter (MVP) paradigm (on the client side alone) along with breaking the UI components into digestible chunks to be able to test each component inside its own "driver" page. Most of the logic resides inside the Presenter(s), which is pure JavaScript and therefore can be tested with pure JavaScript unit tests. Individual UI components can be tested with a framework like Selenium and their testing would simply be ensuring that they throw the right events to the Presenter. The Model can also be made testable as pure-JavaScript if the backend access part is stubbed/mocked out into some sort of Fetcher class.
您可以使用模板引擎。 对于许多不同的 http 服务器设置,存在许多不同的模板引擎,因此您可能会找到一种适合您的需求,并且以您选择的(服务器端)编程语言实现的模板引擎。
现在,一旦安装了模板引擎,您就可以使用原始布局并添加一个小标记,您可以在其中插入
这种方法的优点:生产页面和测试页面之间不需要绝对没有差异。 您可以避免添加除了测试之外不需要的 iframe。
You can use a template engine. Many different template engines exist for many different http server setups, so you'll probably find one that suits your needs, and that is implemented in the (server side) programming language of your choice.
Now once you installed your template engine, you can use your original layout and add a little marker, where you can insert a <script> tag when testing, and nothing when not testing.
The advantage of this approach: there needs to be absolutely no difference between the production pages and the test pages. You can avoid adding an iframe you don't need for anything else than testing.
Windmill 是一个优秀的基于Python的JavaScript测试框架。 它正在积极开发中,因此正在不断改进。 我建议看一下。 您可以创建自动化测试,它有一个很好的 GUI,您也可以使用它来设置测试。 还有很多其他功能,但没有理由在这里全部列出。
Windmill is an excellent python based javascript test framework. It's under active development, so it's constantly improving. I would recommend taking a look at it. You can create automated tests it has a nice GUI you also can use to setup tests. There's a bunch of other features, but no reason to list them all here.
所以您似乎要问两种测试。 一种是对您编写的使用 jquery 的代码进行单元测试。 这个问题的答案比大家通常想象的要简单得多。 只需在单元测试中模拟 jquery 即可。 它非常简单,并且工作方式与使用任何其他编程语言进行模拟的方式几乎相同。 不过即使没有 javascript 框架也很容易做到。 实际上,我使用 Test.TAP 在自动化测试套件中运行这些类型的测试和犀牛为我的项目。 无需浏览器。
另一种测试是集成或回归测试。 这就是像 selenium 这样的工具的用武之地。这些测试发生在您的实际应用程序中,旨在确保所有部分按照您期望的方式协同工作。 浏览器、JQuery、JavaScript 和 Web 服务器。 将这两种测试分别记在心里是将 javascript 代码的测试套件组合在一起的关键。 理想情况下,您可以在多个浏览器(甚至可能是浏览器的多个配置)中运行这些测试。
So there are two kinds of tests you seem to be asking about. One is unittesting the code you write that consumes jquery. The answer to this is much simpler than everyone usually thinks. Just mock jquery in your unit tests. It's really easy and works pretty much the same way you would do mocking in any other programming language. Except it's easy to do without even a framework in javascript. I actually run these kinds of tests in an automated test suite using Test.TAP and rhino for my projects. No browser required.
The other kind of test is an integration or regression test. This is where a tool like selenium comes in. These tests happen in your actual application and are designed to make sure that all the pieces work together the way your expect. Browser, JQuery, your javascript, and Web-Server. Keeping these two kinds of tests seperate in your mind is key to putting together a test suite for javascript code. Ideally you would run these tests in multiple browsers (possibly even multiple configurations of the browsers).
visibone 浏览器参考文献提出了他们所谓的“自信”。 它基本上给出了如何实现 JavaScript 单元测试的建议。 它真的很简单。
见右下图片链接:
http://www.visibone.com/ products/ebk8-9_850.jpg
总结如下:
创建一个函数assert():
你可以这样称呼它:
或者可能有更好的 === 比较:
链接图片上有更多内容,并且您的断言函数可能会变得更加复杂。
我在谷歌上搜索了“JavaScript断言”,并得到了很多链接,所以可能值得你自己检查一下。
The visibone browser reference suggests something they call 'assertiveness'. It basically gives a suggestion on how to implement unit testing for JavaScript. Its really quite simple.
See the bottom right of the following picture link:
http://www.visibone.com/products/ebk8-9_850.jpg
To summarise here:
Create a function assert():
And you could call it something like this:
Or possibly with a better === comparison:
There is more on the linked picture, and your assert functions could become more sophisticaed.
I did a google search on "JavaScript assert", and got quite a few links back, so it might be worth checking out yourself.