Run all unit tests in IntelliJ with RegEx pattern that excludes two different kinds of tests

Viewed 127

I want to add a Run Configuration in IntelliJ IDEA which allows me to only run unit tests and ignores two other types of tests present in the project: integration tests and frontend tests. The integration tests in our project follow the naming pattern NameOfTestClassIT, whereas the frontend tests follow the naming pattern T00_00_NameOfTestClass. I've found a nice solution here to exclude integration tests from being executed and I wanted to extend the RegEx ^(?!.*IT$).*$ with an alternation to exclude the other naming pattern as well.

I tried a lot of different combinations and it resulted in this RegEx: ^(?!(.*IT$)|T[0-9]{2}).*$

I tried to use a RegEx checker to prove that the expression works, and it works perfectly. But when I execute the Run Configuration with this pattern, integration tests are skipped, but not so the frontend tests. Are there any limitations set by IntelliJ that it simply doesn't work or is my RegEx not doing what it should?

I would be glad if someone could help me on that!

1 Answers

Use .* with a negative look behind at the end for IT tests and a negative look ahead at the start for the front end tests:

^(?!T\\d+_).*(?<!IT)$

See live demo.

The look arounds mean:

  • ^(?!T\\d+_) “start is not followed by T then digits then underscore”
  • (?<!IT)$ “the characters preceding end are not IT”

You can vary the first look ahead to be more or less restrictive as suits you.

Related