我的 qUnit 测试套件需要许多不同的全页 DOM 示例

发布于 2024-10-27 07:46:44 字数 689 浏览 2 评论 0原文

我有少量的 Javascript 需要测试,但它在整个页面上运行,例如,通过编号 id 查找元素,如“#t34”。我需要创建一些不同的页面来测试不同的可能配置。我看到我可以使用 qunit-fixture 创建一个 DOM 树以供测试访问,但每个页面配置都需要是一个完整的页面,因为它将通过 id 查找元素。

各种 qUnit 教程似乎都专注于执行简单算术的纯 Javascript 函数的简单示例。我需要帮助了解如何使用许多不同的文件构建真正的测试套件。

更新了更多详细信息:我是 Python 代码覆盖工具 coverage.py 的所有者。它生成 HTML 报告。这些报告使用 jQuery 来添加一点交互性。我添加了很多内容,并且希望自动化测试。 HTML 看起来像这样。不涉及服务器,这些只是写入本地目录的文件,因此没有 Ajax 的机会。我想针对许多不同的页面配置运行测试,主要与红色和绿色代码块的边缘情况有关。

因为我的代码按 $("#t123") 的编号访问源代码行(例如),所以在同一个 DOM 中我不能有多个 HTML“页面”,因为 id 会冲突。如何针对不同的 HTML 页面运行 qUnit?

I have a small amount of Javascript to test, but it operates on the entire page, for example, finding elements by numbered ids like "#t34". I need to create a handful of different pages to test the different possible configurations. I see that I can use qunit-fixture to create a DOM tree for the tests to access, but each page configuration needs to be a complete page, since it will find elements by id.

The various qUnit tutorials out there seem focused on the simple examples of pure Javascript functions performing simple arithmetic. I need help understanding how to build a real test suite using a number of different files.

Updated with more details: I'm the owner of coverage.py, the Python code coverage tool. It generates HTML reports. These reports have jQuery on them to add a little bit of interactivity. I'm adding quite a lot more, and would like to automate the testing. The HTML looks like this. There is no server involved, these are just files written to a local directory, so there is no opportunity for ajax. I would like to run my tests against a number of different page configurations, mostly to do with edge cases of red- and green-colored chunks of code.

Because my code accesses source lines by number as $("#t123") (for example), I can't have more than one "page" of HTML in the same DOM, since the ids will conflict. How can I run qUnit against different pages of HTML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-11-03 07:46:44

#qunit-fixture 由 QUnit 自动重置,这意味着您可以用每个测试使用的标记填充它,或者将其留空并让每个测试填充它以利用自动休息。或者只使用每次测试后单独重置的标记。

您可以使用模块方法重构每个测试的设置和拆卸:http://docs.jquery.com/QUnit/module

在这种情况下,听起来一个不错的选择是对每个测试使用(同步)ajax 请求,将所需的页面标记加载到 #qunit-fixture 元素中。这样你只需要自定义设置,因为 QUnit 之后会重置。

即使您只处理文件系统中的本地文件,只要它们与测试套件文件位于同一目录中,您就应该能够通过 ajax 加载它们。

如果没有,很容易让本地服务器运行,例如通过“open http://localhost:8080/ && python - m SimpleHTTPServer 8080”在该目录中。

#qunit-fixture is automatically reset by QUnit, which means you can fill it with markup used by each test, or leave it empty and have each test fill it to leverage the auto rest. Or just use markup that you reset individually after each test.

You can refactor per-test setup and teardown using the module method: http://docs.jquery.com/QUnit/module

In this case it sounds like a good option would be using (synchronous) ajax requests for each test, loading the page markup you need into the #qunit-fixture element. That way you only need custom setup, as QUnit will reset afterwards.

Even if you're dealing just with local files from the filesystem, as long as they are in the same directory as your testsuite file, you should be able to load them via ajax.

If not, its easy enough to get a local server running, e.g. via "open http://localhost:8080/ && python -m SimpleHTTPServer 8080" in that directory.

陪我终i 2024-11-03 07:46:44

为了测试整个页面和 DOM,我会使用 Selenium http://seleniumhq.org/ 之类的东西
由于您对 JS 测试有一定的了解,您也可以考虑 http://www.phantomjs.org/http://zombie.labnotes.org/

通过这些,您基本上可以使用 Node.js 服务器来加载页面,您可以通过 node.js 命令行或通过脚本完全访问 DOM。可以把它想象成命令行上的 firebug 脚本(或者,firebug 插件也可以做同样的事情)。您可以执行断言来查看您的 id'ed 元素是否存在,并切换单击元素等。

(顺便说一句,我也在 bocoup :D)

For testing a full page and DOM, I would use something like Selenium http://seleniumhq.org/
Since you have some familiarity with JS testing, you might also consider http://www.phantomjs.org/ and http://zombie.labnotes.org/

With these you basically use a node.js server to load the page, from which you have complete access to the DOM at the node.js commandline or through scripting. Think of it like firebug scripting on the commandline (alternatively, a firebug plugin can do the same thing). You can do asserts to see that your id'ed elements exist, and toggle click elements and such.

(btw I was at bocoup, too :D)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文