Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Der `describe` Block | Verwendung eines Testautomatisierungs-Frameworks
Einführung in die QA-Automatisierungstests
course content

Kursinhalt

Einführung in die QA-Automatisierungstests

Einführung in die QA-Automatisierungstests

1. Einführung in das Automatisierte Testen
2. Verwendung eines Testautomatisierungs-Frameworks
3. Browser-Automatisierung mit Selenium
4. Einführung in das Mittlere Automatisierungstesten

book
Der `describe` Block

Zusammenfassung

Manchmal haben wir viele Tests in einem einzigen Testskript, was unorganisiert und schwer zu verwalten sein kann. Zum Beispiel erstellen wir eine neue Datei namens math.js mit einer Math-Klasse darin:

class Math {
	static add(a, b) {
		return a + b;
	}
	
	static subtract(a, b) {
		return a - b;
	}
	
	static multiply(a, b) {
		return a * b;
	}
	
	static divide(a, b) {
		return a / b;
	}
}

module.exports = Math;

Diese Klasse enthält vier Methoden und wir schreiben mehrere Testfälle für jede Methode. Da die Testfälle zu einer einzigen Klasse gehören, würden wir konventionell alle damit verbundenen Testfälle in einer einzigen Datei namens math.test.js speichern:

const Math = require('./math');

test('add two numbers', () => {
    expect(Math.add(1, 2)).toBe(3);
    expect(Math.add(-1, 1)).toBe(0);
    expect(Math.add(-1, -1)).toBe(-2);
});

test('add zero to a number', () => {
    expect(Math.add(0, 0)).toBe(0);
    expect(Math.add(5, 0)).toBe(5);
    expect(Math.add(0, 5)).toBe(5);
});

test('return the difference between two numbers', () => {
    expect(Math.subtract(5, 3)).toBe(2);
    expect(Math.subtract(3, 5)).toBe(-2);
    expect(Math.subtract(-1, -1)).toBe(0);
});

test('handle subtracting zero', () => {
    expect(Math.subtract(5, 0)).toBe(5);
    expect(Math.subtract(0, 5)).toBe(-5);
    expect(Math.subtract(0, 0)).toBe(0);
});

test('return the product of two numbers', () => {
    expect(Math.multiply(2, 3)).toBe(6);
    expect(Math.multiply(-2, 3)).toBe(-6);
    expect(Math.multiply(-2, -3)).toBe(6);
});

test('handle multiplying by zero', () => {
    expect(Math.multiply(0, 5)).toBe(0);
    expect(Math.multiply(5, 0)).toBe(0);
    expect(Math.multiply(0, 0)).toBe(0);
});

test('return the quotient of two numbers', () => {
    expect(Math.divide(6, 3)).toBe(2);
    expect(Math.divide(-6, 3)).toBe(-2);
    expect(Math.divide(-6, -3)).toBe(2);
});

test('handle dividing by one', () => {
    expect(Math.divide(5, 1)).toBe(5);
    expect(Math.divide(-5, 1)).toBe(-5);
});

Wie Sie jedoch bemerken werden, kann dies verwirrend und unorganisiert werden, da es Testfälle für mehrere verschiedene Methoden enthält. Wenn wir die obigen Tests mit dem verbose-Flag ausführen, werden Sie sehen, dass die Tests alle in einer Sequenz ohne Unterscheidung aufgereiht sind.

In solchen Situationen ist es unerlässlich, die zusammengehörigen Testfälle mit describe-Blöcken zu gruppieren:

// General Syntax: describe(text, callback);

describe('summary of the contained test cases', () => {
    // Test Cases Here
});

Mit der obigen Syntax können wir die Testfälle jeder Methode gruppieren:

const Math = require('./math');

describe('add method', () => {
    test('add two numbers', () => {
        expect(Math.add(1, 2)).toBe(3);
        expect(Math.add(-1, 1)).toBe(0);
        expect(Math.add(-1, -1)).toBe(-2);
    });
    
    test('add zero to a number', () => {
        expect(Math.add(0, 0)).toBe(0);
        expect(Math.add(5, 0)).toBe(5);
        expect(Math.add(0, 5)).toBe(5);
    });
});

describe('subtract method', () => {
    test('return the difference between two numbers', () => {
        expect(Math.subtract(5, 3)).toBe(2);
        expect(Math.subtract(3, 5)).toBe(-2);
        expect(Math.subtract(-1, -1)).toBe(0);
    });
    
    test('handle subtracting zero', () => {
        expect(Math.subtract(5, 0)).toBe(5);
        expect(Math.subtract(0, 5)).toBe(-5);
        expect(Math.subtract(0, 0)).toBe(0);
    });
});

describe('multiply method', () => {
    test('return the product of two numbers', () => {
        expect(Math.multiply(2, 3)).toBe(6);
        expect(Math.multiply(-2, 3)).toBe(-6);
        expect(Math.multiply(-2, -3)).toBe(6);
    });
    
    test('handle multiplying by zero', () => {
        expect(Math.multiply(0, 5)).toBe(0);
        expect(Math.multiply(5, 0)).toBe(0);
        expect(Math.multiply(0, 0)).toBe(0);
    });
});

describe('divide method', () => {
    test('return the quotient of two numbers', () => {
        expect(Math.divide(6, 3)).toBe(2);
        expect(Math.divide(-6, 3)).toBe(-2);
        expect(Math.divide(-6, -3)).toBe(2);
    });
    
    test('handle dividing by one', () => {
        expect(Math.divide(5, 1)).toBe(5);
        expect(Math.divide(-5, 1)).toBe(-5);
    });
});

Jetzt sieht der Code organisierter aus, und wenn wir die Tests mit dem verbose-Flag ausführen, werden Sie feststellen, dass die Ausgabe ebenfalls hierarchischer ist, was es einfacher macht, die verschiedenen Testfälle für jede Methode zu sehen.

Es ist auch wichtig zu beachten, dass wir describe-Blöcke verschachteln können, um die Hierarchie der Testfälle weiter zu organisieren:

const Math = require('./math');

describe('Testing Math Class', () => {
    describe('add method', () => {
        test('add two numbers', () => {
            expect(Math.add(1, 2)).toBe(3);
            expect(Math.add(-1, 1)).toBe(0);
            expect(Math.add(-1, -1)).toBe(-2);
        });
        
        test('add zero to a number', () => {
            expect(Math.add(0, 0)).toBe(0);
            expect(Math.add(5, 0)).toBe(5);
            expect(Math.add(0, 5)).toBe(5);
        });
    });

    describe('subtract method', () => {
        test('return the difference between two numbers', () => {
            expect(Math.subtract(5, 3)).toBe(2);
            expect(Math.subtract(3, 5)).toBe(-2);
            expect(Math.subtract(-1, -1)).toBe(0);
        });
        
        test('handle subtracting zero', () => {
            expect(Math.subtract(5, 0)).toBe(5);
            expect(Math.subtract(0, 5)).toBe(-5);
            expect(Math.subtract(0, 0)).toBe(0);
        });
    });

    describe('multiply method', () => {
        test('return the product of two numbers', () => {
            expect(Math.multiply(2, 3)).toBe(6);
            expect(Math.multiply(-2, 3)).toBe(-6);
            expect(Math.multiply(-2, -3)).toBe(6);
        });
        
        test('handle multiplying by zero', () => {
            expect(Math.multiply(0, 5)).toBe(0);
            expect(Math.multiply(5, 0)).toBe(0);
            expect(Math.multiply(0, 0)).toBe(0);
        });
    });

    describe('divide method', () => {
        test('return the quotient of two numbers', () => {
            expect(Math.divide(6, 3)).toBe(2);
            expect(Math.divide(-6, 3)).toBe(-2);
            expect(Math.divide(-6, -3)).toBe(2);
        });
        
        test('handle dividing by one', () => {
            expect(Math.divide(5, 1)).toBe(5);
            expect(Math.divide(-5, 1)).toBe(-5);
        });
    });
});

Die describe-Blöcke helfen auch dabei, den Umfang jedes Testfalls zu definieren. Wir werden einige Verwendungen der Umfänge von Testfällen in späteren Themen sehen.

Abgesehen davon, da wir typischerweise Testfälle in einem describe-Block gruppieren, wird jeder describe-Block in diesem Fall als Test Suite bezeichnet. Es ist wichtig zu beachten, dass in anderen Programmiersprachen ein ganzes Test Script typischerweise als Test Suite bezeichnet wird.

question mark

Wofür kann der describe-Block in Jest verwendet werden? (Wählen Sie alle zutreffenden aus)

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 6

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

course content

Kursinhalt

Einführung in die QA-Automatisierungstests

Einführung in die QA-Automatisierungstests

1. Einführung in das Automatisierte Testen
2. Verwendung eines Testautomatisierungs-Frameworks
3. Browser-Automatisierung mit Selenium
4. Einführung in das Mittlere Automatisierungstesten

book
Der `describe` Block

Zusammenfassung

Manchmal haben wir viele Tests in einem einzigen Testskript, was unorganisiert und schwer zu verwalten sein kann. Zum Beispiel erstellen wir eine neue Datei namens math.js mit einer Math-Klasse darin:

class Math {
	static add(a, b) {
		return a + b;
	}
	
	static subtract(a, b) {
		return a - b;
	}
	
	static multiply(a, b) {
		return a * b;
	}
	
	static divide(a, b) {
		return a / b;
	}
}

module.exports = Math;

Diese Klasse enthält vier Methoden und wir schreiben mehrere Testfälle für jede Methode. Da die Testfälle zu einer einzigen Klasse gehören, würden wir konventionell alle damit verbundenen Testfälle in einer einzigen Datei namens math.test.js speichern:

const Math = require('./math');

test('add two numbers', () => {
    expect(Math.add(1, 2)).toBe(3);
    expect(Math.add(-1, 1)).toBe(0);
    expect(Math.add(-1, -1)).toBe(-2);
});

test('add zero to a number', () => {
    expect(Math.add(0, 0)).toBe(0);
    expect(Math.add(5, 0)).toBe(5);
    expect(Math.add(0, 5)).toBe(5);
});

test('return the difference between two numbers', () => {
    expect(Math.subtract(5, 3)).toBe(2);
    expect(Math.subtract(3, 5)).toBe(-2);
    expect(Math.subtract(-1, -1)).toBe(0);
});

test('handle subtracting zero', () => {
    expect(Math.subtract(5, 0)).toBe(5);
    expect(Math.subtract(0, 5)).toBe(-5);
    expect(Math.subtract(0, 0)).toBe(0);
});

test('return the product of two numbers', () => {
    expect(Math.multiply(2, 3)).toBe(6);
    expect(Math.multiply(-2, 3)).toBe(-6);
    expect(Math.multiply(-2, -3)).toBe(6);
});

test('handle multiplying by zero', () => {
    expect(Math.multiply(0, 5)).toBe(0);
    expect(Math.multiply(5, 0)).toBe(0);
    expect(Math.multiply(0, 0)).toBe(0);
});

test('return the quotient of two numbers', () => {
    expect(Math.divide(6, 3)).toBe(2);
    expect(Math.divide(-6, 3)).toBe(-2);
    expect(Math.divide(-6, -3)).toBe(2);
});

test('handle dividing by one', () => {
    expect(Math.divide(5, 1)).toBe(5);
    expect(Math.divide(-5, 1)).toBe(-5);
});

Wie Sie jedoch bemerken werden, kann dies verwirrend und unorganisiert werden, da es Testfälle für mehrere verschiedene Methoden enthält. Wenn wir die obigen Tests mit dem verbose-Flag ausführen, werden Sie sehen, dass die Tests alle in einer Sequenz ohne Unterscheidung aufgereiht sind.

In solchen Situationen ist es unerlässlich, die zusammengehörigen Testfälle mit describe-Blöcken zu gruppieren:

// General Syntax: describe(text, callback);

describe('summary of the contained test cases', () => {
    // Test Cases Here
});

Mit der obigen Syntax können wir die Testfälle jeder Methode gruppieren:

const Math = require('./math');

describe('add method', () => {
    test('add two numbers', () => {
        expect(Math.add(1, 2)).toBe(3);
        expect(Math.add(-1, 1)).toBe(0);
        expect(Math.add(-1, -1)).toBe(-2);
    });
    
    test('add zero to a number', () => {
        expect(Math.add(0, 0)).toBe(0);
        expect(Math.add(5, 0)).toBe(5);
        expect(Math.add(0, 5)).toBe(5);
    });
});

describe('subtract method', () => {
    test('return the difference between two numbers', () => {
        expect(Math.subtract(5, 3)).toBe(2);
        expect(Math.subtract(3, 5)).toBe(-2);
        expect(Math.subtract(-1, -1)).toBe(0);
    });
    
    test('handle subtracting zero', () => {
        expect(Math.subtract(5, 0)).toBe(5);
        expect(Math.subtract(0, 5)).toBe(-5);
        expect(Math.subtract(0, 0)).toBe(0);
    });
});

describe('multiply method', () => {
    test('return the product of two numbers', () => {
        expect(Math.multiply(2, 3)).toBe(6);
        expect(Math.multiply(-2, 3)).toBe(-6);
        expect(Math.multiply(-2, -3)).toBe(6);
    });
    
    test('handle multiplying by zero', () => {
        expect(Math.multiply(0, 5)).toBe(0);
        expect(Math.multiply(5, 0)).toBe(0);
        expect(Math.multiply(0, 0)).toBe(0);
    });
});

describe('divide method', () => {
    test('return the quotient of two numbers', () => {
        expect(Math.divide(6, 3)).toBe(2);
        expect(Math.divide(-6, 3)).toBe(-2);
        expect(Math.divide(-6, -3)).toBe(2);
    });
    
    test('handle dividing by one', () => {
        expect(Math.divide(5, 1)).toBe(5);
        expect(Math.divide(-5, 1)).toBe(-5);
    });
});

Jetzt sieht der Code organisierter aus, und wenn wir die Tests mit dem verbose-Flag ausführen, werden Sie feststellen, dass die Ausgabe ebenfalls hierarchischer ist, was es einfacher macht, die verschiedenen Testfälle für jede Methode zu sehen.

Es ist auch wichtig zu beachten, dass wir describe-Blöcke verschachteln können, um die Hierarchie der Testfälle weiter zu organisieren:

const Math = require('./math');

describe('Testing Math Class', () => {
    describe('add method', () => {
        test('add two numbers', () => {
            expect(Math.add(1, 2)).toBe(3);
            expect(Math.add(-1, 1)).toBe(0);
            expect(Math.add(-1, -1)).toBe(-2);
        });
        
        test('add zero to a number', () => {
            expect(Math.add(0, 0)).toBe(0);
            expect(Math.add(5, 0)).toBe(5);
            expect(Math.add(0, 5)).toBe(5);
        });
    });

    describe('subtract method', () => {
        test('return the difference between two numbers', () => {
            expect(Math.subtract(5, 3)).toBe(2);
            expect(Math.subtract(3, 5)).toBe(-2);
            expect(Math.subtract(-1, -1)).toBe(0);
        });
        
        test('handle subtracting zero', () => {
            expect(Math.subtract(5, 0)).toBe(5);
            expect(Math.subtract(0, 5)).toBe(-5);
            expect(Math.subtract(0, 0)).toBe(0);
        });
    });

    describe('multiply method', () => {
        test('return the product of two numbers', () => {
            expect(Math.multiply(2, 3)).toBe(6);
            expect(Math.multiply(-2, 3)).toBe(-6);
            expect(Math.multiply(-2, -3)).toBe(6);
        });
        
        test('handle multiplying by zero', () => {
            expect(Math.multiply(0, 5)).toBe(0);
            expect(Math.multiply(5, 0)).toBe(0);
            expect(Math.multiply(0, 0)).toBe(0);
        });
    });

    describe('divide method', () => {
        test('return the quotient of two numbers', () => {
            expect(Math.divide(6, 3)).toBe(2);
            expect(Math.divide(-6, 3)).toBe(-2);
            expect(Math.divide(-6, -3)).toBe(2);
        });
        
        test('handle dividing by one', () => {
            expect(Math.divide(5, 1)).toBe(5);
            expect(Math.divide(-5, 1)).toBe(-5);
        });
    });
});

Die describe-Blöcke helfen auch dabei, den Umfang jedes Testfalls zu definieren. Wir werden einige Verwendungen der Umfänge von Testfällen in späteren Themen sehen.

Abgesehen davon, da wir typischerweise Testfälle in einem describe-Block gruppieren, wird jeder describe-Block in diesem Fall als Test Suite bezeichnet. Es ist wichtig zu beachten, dass in anderen Programmiersprachen ein ganzes Test Script typischerweise als Test Suite bezeichnet wird.

question mark

Wofür kann der describe-Block in Jest verwendet werden? (Wählen Sie alle zutreffenden aus)

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 6
some-alt