Transcripted Summary

During our work for the project, we will create a whole lot of tests, but we don't want to run all of them at once.

In many situations, we want to select a few tests and run only those.

Maybe we want to perform a sanity test, for example, and then we need to extract only those tests that perform the sanity for us.

In other situations, we might want to only select tests that relate to a certain feature.

In order to know which these tests are, we can assign tags to them, and the tag is nothing more than a string which will be assigned to the test through an annotation called a tag.

This annotation will help us include or exclude tests from a test run that we want to perform.

As usual, I will create a new test class, and this time I'm going to, again, copy the FirstTestClass and I'm going to paste it here under the name TaggedTestClass.

Inside this class, we will have the beforeAll, beforeEach, afterAll, and afterEach methods, and we will also have the firstMethod and the secondMethod.

I will create a third one and I will call it simply thirdMethod, and again, I will do a System.out saying that "This is the third test method".

I will also add a parameterized test here because I want to keep it a bit more varied, so I will go to the class where we had the ParameterizedTests, and I will just choose the first one that we have here, and I will copy-paste it and I will place it inside the TaggedTestClass.

Now, in this chapter, we are going over tags, so in order to define a tag, either on a test method or a test class, the only thing we need to do is to add the @Tag annotation.

Now, keep in mind that when we are adding the @Tag annotation, we cannot leave it empty, so we need to provide a string value to this annotation.

This string value has a few requirements.

For example, it cannot be null and it cannot be empty.

Also, it has some requirements regarding not allowing certain characters.

For example, we cannot have round parentheses (), or round brackets, inside the tag value, and we cannot have the and operator &, which is the ampersand, the pipe operator |, which is the or operator, and we can also not have the exclamation mark !, which is normally used for booleans, for example, for negating the condition, so when we are using the not operator, we are using the exclamation mark.

These are a few of the restrictions we have.

In order not to complicate the work very much, when we create tags, it would be ideal if we could create them having alphanumerical or even alphabetical values.

Tagging a method means nothing more than attaching a string value.

What we are going to do with these tags is just signal that we want these tags to have a meaning.

We want the test methods that we are annotating to belong to a certain category.

For example, we want the first method to belong to the category of "sanity" tests.

When we are adding a tag to a test, this will allow us to select the test or exclude that test from a test run based on the value of the string that we are providing.

For example, we can select all of the tests that have the tag "sanity", or we can exclude all of the tests that have the tag "sanity".

We will take a look at that in the chapter where we are running the tests, but for now in this chapter, we will only focus on adding tags to all of these tests that we have here.

For example, for the secondMethod, we will add the tag "sanity", but we will also add another tag and we're going to say "acceptance".

Notice that any new tag that we create for a method needs to be on a new line and it needs to be inside a new @Tag annotation, so we cannot provide several values for a tag within the same tag annotation.

It only takes one parameter and that needs to be a string.

Now, I will go ahead and mark the thirdMethod with a tag, which we're going to call only "acceptance".

For the ParameterizedTest, I will also add a tag and this time I will say again "acceptance".


package junit5tests;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TaggedTestClass {

    @BeforeAll
    void beforeAll() {
        System.out.println("--This is the before All method");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("----This is the before Each method");
    }

    @AfterAll
    void afterAll() {
        System.out.println("--This is the after All method");
    }

    @AfterEach
    void afterEach() {
        System.out.println("----This is the after Each method");
    }

    @Test
    @Tag("sanity")
    void firstMethod() {
        System.out.println("This is the first test method");
    }

    @Test
    @Tag("sanity")
    @Tag("acceptance")
    @DisplayName("US1234 - TC12 - this method is the second one")
    void secondMethod() {
        System.out.println("This is the second test method");
    }

    @Test
    @Tag("acceptance")
    void thirdMethod() {
        System.out.println("This is the third test method");
    }


    @Tag("acceptance")
    @ParameterizedTest(name = "Run: {index}  -  value: {arguments}")
    @ValueSource(ints = {1, 5, 6})
    void intValues(int theParam) {
        System.out.println("theParam = " + theParam);
    }
}

This is going to help us in the next chapter to run the tests based on the tag that they are assigned.

What we need to remember though, after this chapter, is that we can add a tag to either test methods or test classes, and that those tags cannot have certain characters, and that we can only provide one value as a string to a tag parameter.

If we want several tags, then we will need to create several @Tag annotation lines for each method that we are annotating.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR