Generating display names for @ParameterizedTest in JUnit 5

As of JUnit 5.8.0, there is a Named<T> interface as part of the JUnit Jupiter API with “automatic support for injecting the contained payload [the arguments] into parameterized methods directly” (see issue #2301). Example: @DisplayName(“A parameterized test with named arguments”) @ParameterizedTest @MethodSource(“namedArguments”) void testWithNamedArguments(File file) {} static Stream<Arguments> namedArguments() { return Stream.of( Arguments.of(Named.of(“An important file”, … Read more

indirect=True vs indirect=False in @pytest.mark.parametrize()?

With indirect=True you can parametrize your fixture, False – default value. Example: import pytest @pytest.fixture def fixture_name(request): return request.param @pytest.mark.parametrize(‘fixture_name’, [‘foo’, ‘bar’], indirect=True) def test_indirect(fixture_name): assert fixture_name == ‘baz’ So this example generates two tests. First one gets from fixture_name value foo, because this fixture for this test runs with parametization. Second test gets bar … Read more

jasmine parameterized unit test

Based on piotrek’s answer and the article Parameterized testing in Javascript, you could also use the following approach which uses ES6 syntax: [ [‘abc’, 3], [‘ab’, 2], [”, 0], ].forEach(([string, expectedLength]) => { it(`should return length ${expectedLength} for string “${string}”`, () => { expect(string.length).toBe(expectedLength); }); }); I have tested it with the Jest test framework, … Read more

Google Test: Is there a way to combine a test which is both type parameterized and value parameterized?

There isn’t any ready-to-wear combination of type-parameterized tests and value-parameterized tests. The googletest developers have been asked the question and they said No. However, there is a routine and simple way (as suggested by Zhanyong Wan in the linked discussion) in which you can craft you own type-parameterised test case that tests some condition for … Read more

JUnit5 parameterized tests at class level

Short answer there’s no way to parametrize class creation with JUnit 5 following the style of JUnit 4. Fortunately, the very intention of separation test logic and test input data (parameters) can be implemented differently. JUnit 5 has its own approach for making parameterized tests, and, of course, it is different from JUnit 4. The … Read more

Create multiple parameter sets in one parameterized class (junit)

This answer is similar to Tarek’s one (the parametrized part), although I think it is a bit more extensible. Also solves your problem and you won’t have failed tests if everything is correct: @RunWith(Parameterized.class) public class CalculatorTest { enum Type {SUBSTRACT, ADD}; @Parameters public static Collection<Object[]> data(){ return Arrays.asList(new Object[][] { {Type.SUBSTRACT, 3.0, 2.0, 1.0}, … Read more

MSTest Equivalent for NUnit’s Parameterized Tests?

For those using MSTest2, DataRow + DataTestMethod are available to do exactly this: [DataRow(Enum.Item1, “Name1”, 123)] [DataRow(Enum.Item2, “Name2”, 123)] [DataRow(Enum.Item3, “Name3”, 456)] [DataTestMethod] public void FooTest(EnumType item, string name, string number) { var response = ExecuteYourCode(item, name, number); Assert.AreEqual(item, response.item); } More about it here

How do you generate dynamic (parameterized) unit tests in Python?

This is called “parametrization”. There are several tools that support this approach. E.g.: pytest’s decorator parameterized The resulting code looks like this: from parameterized import parameterized class TestSequence(unittest.TestCase): @parameterized.expand([ [“foo”, “a”, “a”,], [“bar”, “a”, “b”], [“lee”, “b”, “b”], ]) def test_sequence(self, name, a, b): self.assertEqual(a,b) Which will generate the tests: test_sequence_0_foo (__main__.TestSequence) … ok test_sequence_1_bar … Read more