Domanda

I want a unit-test framework for JavaScript which runs in the browser. Not with an external browser-automation system as in Selenium, or a non-browser JS environment, but just one .html which loads the test framework and test suite and displays the results in that page. My searches have found only the other two kinds.

I am primarily interested in testing JavaScript code for expected results and not crashing, not DOM/events or visual appearance.

Nice-to-haves:

  • Performance testing/benchmarking as well as correctness testing.
  • Optional external automation so a shell command can run the test suite and check if it succeeded.

The motivation for the requirements is:

  • My application under test is currently itself pure JavaScript, and I'd like to keep it that way (without platform or other-language dependencies).
  • I am using many newer or bleeding-edge features such as ECMAScript 5, Typed Arrays, WebGL, and Local Storage. I want to test the actual interaction with these facilities, not stubs.
È stato utile?

Soluzione

jsunit or Jasmine

You can run Selenium in the browser only. Download Selenium Core 1.01. Upload the unzipped files to the domain you want to test. Open /core/TestRunner.html and start the browser only frontend of Selenium :)

I use this kind of test in several projects. It's perfect to have only one place to store and to run tests. You can trigger those these by cronjobs and capture the results as well. I extended it a lot and I save the results to a Database and take automatically screenshots with JS when errors occur.

If this kind of running Selenium is still supported in version > 2.0, I don't know. Here is a tiny documentation.

Altri suggerimenti

I recommend qunit. It's created by the jQuery team (and is used to test the jQuery framework) and it works in both the browser and node. It's really quick to set up tests, with just a single HTML file which has script tags with your tests, and it's well documented.

Besides those already mentioned (qunit & Jasmine) I recommend mocha.js, it adds a little bit more complexity but it's so much more flexible, you need to load the file, add a script tag with the configuration and start testing, the plus is you can use any assertion/expectation library you like if it throws exceptions if the test fails.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top