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

Cannot run @ParameterizedTest method invocations in parallel and other test sequentially #3373

Closed
gl-khimin opened this issue Jun 26, 2023 · 6 comments

Comments

@gl-khimin
Copy link

gl-khimin commented Jun 26, 2023

How can I run a parameterized test which retrieves data from @MethodSource in parallel?

  private static Stream<Arguments> scenarioPathList2() {
        return Stream.of(
                Arguments.of("example-data/file1.csv"),
                Arguments.of("example-data/file2.csv"));
    }

    @ParameterizedTest(name = "Parallel Test1 {0}")
    @MethodSource({"scenarioPathList"})
    @Execution(CONCURRENT)
    public void parallelTest1(String scenarioPath) {
        System.out.println("Test1 time: " +LocalDateTime.now());
        executeScenario(scenarioPath);
    }

One way to do it is to run only one test in class parallel. I found global configurations:

  • junit.jupiter.execution.parallel.mode.default = same_thread: run tests in class in parallel
  • junit.jupiter.execution.parallel.mode.classes.default = same_thread: tests classes parallel
  • junit.jupiter.execution.parallel.enabled=true

image

@gl-khimin
Copy link
Author

@marcphilipp does JUnit 5 has the opportunity to run tests from @MethodSource parallely ?

@sormuras
Copy link
Member

sormuras commented Jun 26, 2023

Edit(h) deleted a screenshot of "Writing Tests" / "Parallel Execution" section from the User Guide.

https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution

@gl-khimin
Copy link
Author

@sormuras thanks for the reference, but test with method source feels it as separate test. When I run A.test1[1] and A.test[2] and A.test2[1] and A.test2[2] it can run crossing like A.test1[1] and A.test2[1].

How to be sure that test with all data run and after that move to another test ?

@TestMethodOrder(MethodOrderer.OrderAnnotation.class) doesn't help me.

@sbrannen sbrannen changed the title JUnit5 doesn't allow to run parameterized test with @MethodSource in parallel and other test sequentially Cannot run @ParameterizedTest with @MethodSource in parallel and other test sequentially Sep 8, 2024
@sbrannen
Copy link
Member

sbrannen commented Sep 8, 2024

@gl-khimin, just to be clear, are you asking the following question?

  • Is it possible to configure individual invocations of a @ParameterizedTest method to run concurrently (in parallel)?

@sbrannen sbrannen changed the title Cannot run @ParameterizedTest with @MethodSource in parallel and other test sequentially Cannot run @ParameterizedTest method invocations in parallel and other test sequentially Sep 8, 2024
Copy link

If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.

@marcphilipp
Copy link
Member

marcphilipp commented Sep 23, 2024

@gl-khimin IIUC you're asking if there's a way to run invocations of a parameterized test in parallel but not concurrently to invocations of other parameterized tests, right?

For example, run test1[1]..test1[6] in parallel and then test2[1]..test2[6].

public class ConcurrentParameterizedTests {
	
	@ParameterizedTest
	@ValueSource(ints = { 1, 2, 3, 4, 5, 6 })
	void test1() throws Exception {
		Thread.sleep(1000);
	}

	@ParameterizedTest
	@ValueSource(ints = { 1, 2, 3, 4, 5, 6 })
	void test2() throws Exception {
		Thread.sleep(1000);
	}
}

Currently that's only possible if you enclose each parameterized test in a nested class:

@Execution(SAME_THREAD)
public class ConcurrentParameterizedTests {

	@Nested
	class A {
		@ParameterizedTest
		@Execution(CONCURRENT)
		@ValueSource(ints = { 1, 2, 3, 4, 5, 6 })
		void test1() throws Exception {
			Thread.sleep(1000);
		}
	}

	@Nested
	class B {
		@ParameterizedTest
		@Execution(CONCURRENT)
		@ValueSource(ints = { 1, 2, 3, 4, 5, 6 })
		void test2() throws Exception {
			Thread.sleep(1000);
		}
	}
}

Please let me know if that wasn't what you were asking. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants