2

This is a noob question.

I am going to write a string manipulation function, which has nothing to do with web browsers. Now I would like to test that function.

I know how to do that in Java with JUnit: I write a few test cases and make JUnit run the tests cases manually or automatically (during the build).

In case of Java Script I can write a few test functions, prepare an HTML page, which invokes these functions, and load this page in a browser.

Is it really the only option to do unit testing for Java Script?

Michael
  • 10,185
  • 12
  • 59
  • 110
  • 1
    Maybe this post helps: http://stackoverflow.com/questions/300855/looking-for-a-better-javascript-unit-test-tool – m90 May 03 '12 at 12:57
  • 1
    And maybe this one: http://stackoverflow.com/questions/1692873/javascript-unit-testing-frameworks – James Allardice May 03 '12 at 12:58
  • 1
    [This article](http://tech.pro/tutorial/1246/javascript-unit-testing-in-visual-studio-with-resharper) might be useful to see what other people do: – strangeloops May 13 '13 at 21:49

3 Answers3

2

You can try usi jasmine http://pivotal.github.com/jasmine/ it's a testing framework for JavaScript that can test outside a browser. You'll still need a javascript execution stack on your testing environment though.

Bastes
  • 1,116
  • 1
  • 9
  • 18
1

If you are open to testing in a web browser. I recently wrote a JS unit test class that may help you out. You can run various tests separately or you can execute multiple tests defined in an array with one function. You can see examples of the usage on GitHub.

https://github.com/jakesankey/JSTest

Jake Sankey
  • 4,977
  • 12
  • 39
  • 53
0

No, page output is not the only option. There's another option to output your results to the console (F12 in most browsers will open that up for you). One of the frameworks that outputs to the console in its current version is bob.js.

Tengiz
  • 8,011
  • 30
  • 39