How can I run cleanup method only after tagged tests?

arghtype :

I'm writing JUnit 5 tests for my Java project.

I have some test methods that require time consuming clean up (after each of them). Ideally, I would like to mark them with some annotation and run cleanup method only for them.

This is what I tried:

class MyTest {

    @AfterEach
    @Tag("needs-cleanup")
    void cleanup() {
        //do some complex stuff
    }

    @Test
    void test1() {
         //do test1
    }

    @Test
    @Tag("needs-cleanup")
    void test2() {
         //do test2
    }
}

I want cleanup method to be run only after test2. But it actually runs after both tests.

Is it possible to achieve it via some combination of JUnit 5 annotations? I don't want to split my test class into several classes or call cleanup from test methods directly.

MohammadReza Alagheband :

From Documentation:

TestInfo: if a method parameter is of type TestInfo, the TestInfoParameterResolver will supply an instance of TestInfo corresponding to the current test as the value for the parameter. The TestInfo can then be used to retrieve information about the current test such as the test’s display name, the test class, the test method, or associated tags. The display name is either a technical name, such as the name of the test class or test method, or a custom name configured via @DisplayName.

TestInfo acts as a drop-in replacement for the TestName rule from JUnit 4.

Regarding above description, you can use TestInfo class which gives you information of the class that cleanUp is supposed to be run for,then you need check the condition and allow those you want by checking their tags:

@AfterEach 
void afterEach(TestInfo info) {
    if(!info.getTags().contains("cleanItUp")) return; // preconditioning only to needs clean up
        //// Clean up logic Here
}


@Test
@Tag("cleanItUp")
void myTest() {

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I cleanup a Thread after its run method finishes?

How can I run google death tests in debug only?

How can I run a tagged cucumber test multiple times?

How can I make Mix run only specific tests from my suite of tests?

How can i run only the unit tests not the integration tests on build phase with maven

C# can I run a method that only executes if Unit Tests Pass

How can I run a cleanup script on virtualenv `deactivate`?

How can I run my function only after a key is pressed?

Gitlab-ci How can I trigger a cleanup job only after previous stage succeeded disregarding all other stages status

How can I run kotlintest tests with gradle?

How can I run tests with a headless browser?

How can I run testfx tests in series?

How can I run Jasmine tests with Deno?

How can I ensure tests with a marker are only run if explicitly asked in pytest?

How can I use Maven to run only previously failed JUnit 5 tests?

How can I run integration tests after building all modules in a multi-module Maven project?

In IntelliJ IDEA, can I run only tests matching a regex pattern?

How can we automatically only run tests on newer Android versions?

I run zsh from bash after connecting SSH – how can I exit both with only one command?

How do I run only specific tests in Rspec?

How can can I run tests using qtbot on macos Sonoma?

How can performed method setUp only once in tests

Database cleanup after Junit tests

Cleanup after all junit tests

How can I make only the parent method run in a class inheritance on PHP?

I can't run tests

How to run your tests in class level parallelism only after one of your class has finished with it's tests

Run integration tests on gitlab after merge only

How can I implement Cleanup in TestComplete

TOP Ranking

HotTag

Archive