Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Fix #57: Add section on meta-generated tests

Daniel St. Jules86f85bd66a67
diff --git a/README.md b/README.md
index 50c7c569..a89c9948 100644
--- a/README.md
+++ b/README.md
@@ -16,9 +16,9 @@ Install `markdown` via Homebrew:
```sh
brew install markdown
```
-
+
Or download from [here](http://daringfireball.net/projects/markdown/).
-
+
Celebrate with tequila! Or try to build first. Probably want to build first.
### Linux
diff --git a/index.html b/index.html
index f8307d82..a8c4aa13 100644
--- a/index.html
+++ b/index.html
@@ -72,6 +72,7 @@
<li><a href="#pending-tests">Pending tests</a></li>
<li><a href="#exclusive-tests">Exclusive tests</a></li>
<li><a href="#inclusive-tests">Inclusive tests</a></li>
+<li><a href="#meta-generated-test">Meta-Generated tests</a></li>
<li><a href="#test-duration">Test duration</a></li>
<li><a href="#string-diffs">String diffs</a></li>
<li><a href="#usage">mocha(1)</a></li>
@@ -329,6 +330,46 @@ beforeEach('some description', function(){
})
</code></pre>
+<h2 id="meta-generated-test">Meta-Generated tests</h2>
+
+<p>Given mocha's use of call statements and function expressions to define
+ suites and specs, it's rather straightforward to generate your tests. No
+ special syntax is required - plain JavaScript can be used to achieve
+ similar functionality as parameterized tests in other test frameworks.
+ Take the following example:</p>
+
+<pre><code>var assert = require('assert');
+
+function add() {
+ return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
+ return prev + curr;
+ }, 0);
+}
+
+describe('add()', function() {
+ var tests = [
+ {args: [1, 2], expected: 3},
+ {args: [1, 2, 3], expected: 6},
+ {args: [1, 2, 3, 4], expected: 10}
+ ];
+
+ tests.forEach(function(test) {
+ it('correctly adds ' + test.args.length + ' args', function() {
+ var res = add.apply(null, test.args);
+ assert.equal(res, test.expected);
+ });
+ });
+});
+</code></pre>
+
+<p>The code above will output a suite with three specs:</p>
+
+<pre><code>add()
+ ✓ correctly adds 2 args
+ ✓ correctly adds 3 args
+ ✓ correctly adds 4 args
+</code></pre>
+
<h2 id="test-duration">Test duration</h2>
<p>Most of the reporters support some form of displaying
@@ -785,7 +826,7 @@ testCase('Array', function(){
<p>Mocha options can be set via <code>mocha.setup()</code>. Examples:</p>
-<pre><code>// Use "tdd" interface. This is a shortcut to setting the interface;
+<pre><code>// Use "tdd" interface. This is a shortcut to setting the interface;
// any other options must be passed via an object.
mocha.setup('tdd');
diff --git a/index.md b/index.md
index 8455e147..379a08e3 100644
--- a/index.md
+++ b/index.md
@@ -45,6 +45,7 @@ Mocha is a feature-rich JavaScript test framework running on [node.js](http://no
- [Pending tests](#pending-tests)
- [Exclusive tests](#exclusive-tests)
- [Inclusive tests](#inclusive-tests)
+ - [Meta-Generated tests](#meta-generated-test)
- [Test duration](#test-duration)
- [String diffs](#string-diffs)
- [mocha(1)](#usage)
@@ -290,6 +291,44 @@ Testing asynchronous code with Mocha could not be simpler! Simply invoke the cal
})
})
+<h2 id="meta-generated-test">Meta-Generated tests</h2>
+
+ Given mocha's use of call statements and function expressions to define
+ suites and specs, it's rather straightforward to generate your tests. No
+ special syntax is required - plain JavaScript can be used to achieve
+ similar functionality as parameterized tests in other test frameworks.
+ Take the following example:
+
+ var assert = require('assert');
+
+ function add() {
+ return Array.prototype.slice.call(arguments).reduce(function(prev, curr) {
+ return prev + curr;
+ }, 0);
+ }
+
+ describe('add()', function() {
+ var tests = [
+ {args: [1, 2], expected: 3},
+ {args: [1, 2, 3], expected: 6},
+ {args: [1, 2, 3, 4], expected: 10}
+ ];
+
+ tests.forEach(function(test) {
+ it('correctly adds ' + test.args.length + ' args', function() {
+ var res = add.apply(null, test.args);
+ assert.equal(res, test.expected);
+ });
+ });
+ });
+
+ The code above will output a suite with three specs:
+
+ add()
+ ✓ correctly adds 2 args
+ ✓ correctly adds 3 args
+ ✓ correctly adds 4 args
+
<h2 id="test-duration">Test duration</h2>
Most of the reporters support some form of displaying