有没有办法用Jestjs编写更多的数据驱动测试?
我想出了这样的东西:
it('assignments and declarations', () => {
testCases.forEach(function (testCase) {
const slx = fs.readFileSync(testDirectory + 'slx/' + testCase.slx, 'utf-8');
const document = compiler.parse(slx);
const lmsGenerator = new LMSGenerator(document);
const lms = lmsGenerator.generate();
const expected = fs.readFileSync(testDirectory + 'lms/' + testCase.lms, 'utf-8');
expect(expected).toBe(lms);
});
});我从文件中读取了输入和预期输出。这些文件(以及输入和输出之间的链接)保存在带有对象的数组中。问题是我在使用多个it()函数时丢失了特定的错误消息。
有没有办法做得更好?或者在预期调用将失败的情况下使用单独的消息?
发布于 2016-11-15 07:09:19
您可以为每个测试用例创建一个it块,并将它们捆绑到一个describe块中。现在,您将获得每个失败案例的错误消息,并且测试不会在第一次失败后停止。
describe('assignments and declarations for', () => {
testCases.forEach(function (testCase) {
it(`case ${testCase}`, () => {
const slx = fs.readFileSync(testDirectory + 'slx/' + testCase.slx, 'utf-8');
const document = compiler.parse(slx);
const lmsGenerator = new LMSGenerator(document);
const lms = lmsGenerator.generate();
const expected = fs.readFileSync(testDirectory + 'lms/' + testCase.lms, 'utf-8');
expect(expected).toBe(lms);
});
});
});https://stackoverflow.com/questions/40598985
复制相似问题