Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated testing with Mocha #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions 1-js/03-code-quality/05-testing-mocha/3-pow-test-wrong/solution.md
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
The test demonstrates one of the temptations a developer meets when writing tests.
Der Test zeigt eine der Versuchungen, denen ein Entwickler beim Schreiben von Tests begegnet.

What we have here is actually 3 tests, but layed out as a single function with 3 asserts.
Was wir hier tatsächlich haben, sind eigentlich 3 Tests, aber angeordnet als einzelne Funktion mit 3 Assertions.

Sometimes it's easier to write this way, but if an error occurs, it's much less obvious what went wrong.
Manchmal ist es einfacher, auf diese Weise zu schreiben, aber wenn ein Fehler auftritt, ist es viel weniger offensichtlich, was schiefgelaufen ist.

If an error happens in the middle of a complex execution flow, then we'll have to figure out the data at that point. We'll actually have to *debug the test*.
Tritt ein Fehler in der Mitte eines komplexen Programmablaufs auf, dann müssen wir die Daten an diesem Punkt herausfinden. Wir müssen tatsächlich *den Test debuggen*.

It would be much better to break the test into multiple `it` blocks with clearly written inputs and outputs.
Es wäre viel besser, den Test in mehrere `it` Blöcke aufzubrechen, mit klar geschriebenen Eingaben und Ausgaben.

Like this:
So zum Beispiel:
```js
describe("Raises x to power n", function() {
it("5 in the power of 1 equals 5", function() {
describe("Erhöht x zur Potenz n", function() {
it("5 in der Potenz 1 gleich 5", function() {
assert.equal(pow(5, 1), 5);
});

it("5 in the power of 2 equals 25", function() {
it("5 in der Potenz 2 gleich 25", function() {
assert.equal(pow(5, 2), 25);
});

it("5 in the power of 3 equals 125", function() {
it("5 in der Potenz 3 gleich 125", function() {
assert.equal(pow(5, 3), 125);
});
});
```

We replaced the single `it` with `describe` and a group of `it` blocks. Now if something fails we would see clearly what the data was.
Wir haben das einzelnen `it` durch `describe` und eine Gruppe von `it` Blöcken ersetzt. Wenn jetzt etwas fehlschlägt, würden wir klar sehen, welche Daten es waren.

Also we can isolate a single test and run it in standalone mode by writing `it.only` instead of `it`:
Außerdem können wir einen einzelnen Test isolieren und ihn im Standalone-Modus ausführen, indem wir `it.only` statt `it` schreiben:


```js
describe("Raises x to power n", function() {
it("5 in the power of 1 equals 5", function() {
describe("Erhöht x zur Potenz n", function() {
it("5 in der Potenz 1 gleich 5", function() {
assert.equal(pow(5, 1), 5);
});

*!*
// Mocha will run only this block
it.only("5 in the power of 2 equals 25", function() {
// Mocha wird nur diesen Block ausführen
it.only("5 in der Potenz 2 gleich 25", function() {
assert.equal(pow(5, 2), 25);
});
*/!*

it("5 in the power of 3 equals 125", function() {
it("5 in der Potenz 3 gleich 125", function() {
assert.equal(pow(5, 3), 125);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ importance: 5

---

# What's wrong in the test?
# Was ist falsch mit dem Test?

What's wrong in the test of `pow` below?
Was ist falsch bei dem Test von `pow` unten?

```js
it("Raises x to the power n", function() {
it("Erhebt x zur Potenz n", function() {
let x = 5;

let result = x;
Expand All @@ -21,4 +21,4 @@ it("Raises x to the power n", function() {
});
```

P.S. Syntactically the test is correct and passes.
P.S. Syntaxmäßig ist der Test korrekt und besteht.
312 changes: 156 additions & 156 deletions 1-js/03-code-quality/05-testing-mocha/article.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ describe("test", function() {

// This is because of the "alert" function, because if you delay pressing the "OK" button the tests will not pass!

before(() => alert("Testing startedbefore all tests"));
after(() => alert("Testing finishedafter all tests"));
before(() => alert("Test beginntvor allen Tests"));
after(() => alert("Test abgeschlossennach allen Tests"));

beforeEach(() => alert("Before a testenter a test"));
afterEach(() => alert("After a testexit a test"));
beforeEach(() => alert("Vor einem Testbeginne einen Test"));
afterEach(() => alert("Nach einem Testbeende einen Test"));

it('test 1', () => alert(1));
it('test 2', () => alert(2));
Expand Down
18 changes: 9 additions & 9 deletions 1-js/03-code-quality/05-testing-mocha/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<!-- add mocha css, to show results -->
<!-- Füge das Mocha CSS hinzu, um die Ergebnisse anzuzeigen -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
<!-- add mocha framework code -->
<!-- Füge den Mocha Framework-Code hinzu -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
<script>
mocha.setup('bdd'); // minimal setup
mocha.setup('bdd'); // minimales Setup
</script>
<!-- add chai -->
<!-- Füge Chai hinzu -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<script>
// chai has a lot of stuff, let's make assert global
// Chai hat viele Funktionen, lass uns assert global machen
let assert = chai.assert;
</script>
</head>
Expand All @@ -20,17 +20,17 @@

<script>
function pow(x, n) {
/* function code is to be written, empty now */
/* Funktionscode muss geschrieben werden, ist momentan leer */
}
</script>

<!-- the script with tests (describe, it...) -->
<!-- das Skript mit Tests (describe, it...) -->
<script src="test.js"></script>

<!-- the element with id="mocha" will contain test results -->
<!-- Das Element mit der ID "mocha" wird die Testergebnisse enthalten -->
<div id="mocha"></div>

<!-- run tests! -->
<!-- Führe die Tests aus! -->
<script>
mocha.run();
</script>
Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-1.view/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("pow", function() {

it("raises to n-th power", function() {
it("erhöht auf die n-te Potenz", function() {
assert.equal(pow(2, 3), 8);
});

Expand Down
4 changes: 2 additions & 2 deletions 1-js/03-code-quality/05-testing-mocha/pow-2.view/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe("pow", function() {

it("2 raised to power 3 is 8", function() {
it("2 erhöht auf Potenz 3 ist 8", function() {
assert.equal(pow(2, 3), 8);
});

it("3 raised to power 4 is 81", function() {
it("3 erhöht auf Potenz 4 ist 81", function() {
assert.equal(pow(3, 4), 81);
});

Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-3.view/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe("pow", function() {

function makeTest(x) {
let expected = x * x * x;
it(`${x} in the power 3 is ${expected}`, function() {
it(`${x} zur Potenz 3 ist ${expected}`, function() {
assert.equal(pow(x, 3), expected);
});
}
Expand Down
4 changes: 2 additions & 2 deletions 1-js/03-code-quality/05-testing-mocha/pow-4.view/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe("pow", function() {

describe("raises x to power 3", function() {
describe("erhöht x auf Potenz 3", function() {

function makeTest(x) {
let expected = x * x * x;
it(`${x} in the power 3 is ${expected}`, function() {
it(`${x} zur Potenz 3 ist ${expected}`, function() {
assert.equal(pow(x, 3), expected);
});
}
Expand Down
8 changes: 4 additions & 4 deletions 1-js/03-code-quality/05-testing-mocha/pow-full.view/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe("pow", function() {

describe("raises x to power 3", function() {
describe("erhöht x auf Potenz 3", function() {

function makeTest(x) {
let expected = x * x * x;
it(`${x} in the power 3 is ${expected}`, function() {
it(`${x} zur Potenz 3 is ${expected}`, function() {
assert.equal(pow(x, 3), expected);
});
}
Expand All @@ -15,11 +15,11 @@ describe("pow", function() {

});

it("if n is negative, the result is NaN", function() {
it("für negatives n ist das Ergebnis NaN", function() {
assert.isNaN(pow(2, -1));
});

it("if n is not integer, the result is NaN", function() {
it("für nicht-ganzzahliges n ist das Ergebnis NaN", function() {
assert.isNaN(pow(2, 1.5));
});

Expand Down
2 changes: 1 addition & 1 deletion 1-js/03-code-quality/05-testing-mocha/pow-min.view/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("pow", function() {

it("raises to n-th power", function() {
it("erhöht auf die n-te Potenz", function() {
assert.equal(pow(2, 3), 8);
});

Expand Down
8 changes: 4 additions & 4 deletions 1-js/03-code-quality/05-testing-mocha/pow-nan.view/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
describe("pow", function() {

describe("raises x to power 3", function() {
describe("erhöht x auf Potenz 3", function() {

function makeTest(x) {
let expected = x * x * x;
it(`${x} in the power 3 is ${expected}`, function() {
it(`${x} zur Potenz 3 ist ${expected}`, function() {
assert.equal(pow(x, 3), expected);
});
}
Expand All @@ -15,11 +15,11 @@ describe("pow", function() {

});

it("if n is negative, the result is NaN", function() {
it("für negatives n ist das Ergebnis NaN", function() {
assert.isNaN(pow(2, -1));
});

it("if n is not integer, the result is NaN", function() {
it("für nicht-ganzzahliges n ist das Ergebnis NaN", function() {
assert.isNaN(pow(2, 1.5));
});

Expand Down