使用不同参数多次进行 JS 单元测试 运行
JS Unit testing run multiple times with different parameters
他们是否有任何方法可以在一个测试中使用多个参数而不是再次复制和粘贴函数?
C# 的 NUnit 示例:
[TestCase("0", 1)]
[TestCase("1", 1)]
[TestCase("2", 1)]
public void UnitTestName(string input, int expected)
{
//Arrange
//Act
//Assert
}
我想要的Js:
describe("<Foo />", () => {
[TestCase("false")]
[TestCase("true")]
it("option: enableRemoveControls renders remove controls", (enableRemoveControls) => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
});
您可以将 it
-call 放入函数中并使用不同的参数调用它:
describe("<Foo />", () => {
function run(enableRemoveControls){
it("option: enableRemoveControls renders remove controls", () => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
}
run(false);
run(true);
});
另一种方法是使用 Jest. It has this functionality 内置:
test.each`
a | b | expected
| |
| |
| |
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
如果您使用 Mocha you can combine it with mocha-testdata:
import * as assert from assert;
import { given } from mocha-testdata;
describe('<Foo />', function () {
given([
{ input: true, expected: 'some expected value', description: 'flag enabled' },
{ input: false, expected: 'some other expected value', description: 'flag disabled' },
]).
it('option: enableRemoveControls renders remove controls', function ({ input, expected }) {
// prepare, act, assert
});
});
在上面的示例中,您还会注意到一个 description
字段没有被注入到测试中。
这个小技巧可以用来让 reported test name 更有意义。
希望对您有所帮助!
一月
他们是否有任何方法可以在一个测试中使用多个参数而不是再次复制和粘贴函数?
C# 的 NUnit 示例:
[TestCase("0", 1)]
[TestCase("1", 1)]
[TestCase("2", 1)]
public void UnitTestName(string input, int expected)
{
//Arrange
//Act
//Assert
}
我想要的Js:
describe("<Foo />", () => {
[TestCase("false")]
[TestCase("true")]
it("option: enableRemoveControls renders remove controls", (enableRemoveControls) => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
});
您可以将 it
-call 放入函数中并使用不同的参数调用它:
describe("<Foo />", () => {
function run(enableRemoveControls){
it("option: enableRemoveControls renders remove controls", () => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
}
run(false);
run(true);
});
另一种方法是使用 Jest. It has this functionality 内置:
test.each`
a | b | expected
| |
| |
| |
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
如果您使用 Mocha you can combine it with mocha-testdata:
import * as assert from assert;
import { given } from mocha-testdata;
describe('<Foo />', function () {
given([
{ input: true, expected: 'some expected value', description: 'flag enabled' },
{ input: false, expected: 'some other expected value', description: 'flag disabled' },
]).
it('option: enableRemoveControls renders remove controls', function ({ input, expected }) {
// prepare, act, assert
});
});
在上面的示例中,您还会注意到一个 description
字段没有被注入到测试中。
这个小技巧可以用来让 reported test name 更有意义。
希望对您有所帮助!
一月