M
M
Mikhri2021-12-05 20:56:04
Java
Mikhri, 2021-12-05 20:56:04

How to run single tests in descendants (JUnit 5)?

There are two programs with similar functionality.
One is a more advanced version of the second, if we draw an analogy with a calculator - the first can + -and the second + - * /.

Both need to be tested.
There is a class of JUnit tests: conditionally, TestFirstProgram.
All by the same analogy with a calculator, it is arranged somehow like this:

public class ТестыПервойПрограммы {
  protected тестПосчитать(ожидаемыйРезультат, выражение) {
    assertEquals(ожидаемыйРезультат, new ПерваяПрограмма().посчитать(выражение));
  }

  @Test
  void дваПлюсДва() {
    тестПосчитать(4, "2+2");
  }
}


Only @Test - a few dozen, of course.
In order not to duplicate their code and not bother if something needs to be changed (and I often have to, because this is not an application program, but purely educational, i.e. I write it to teach unit tests), I inherit tests. Something like this:

public class ТестыВторойПрограммы extends ТестыПервойПрограммы {
  
  @Override
  protected тестПосчитать(ожидаемыйРезультат, выражение) {
    assertEquals(ожидаемыйРезультат, new ВтораяПрограмма().посчитать(выражение));
  }

  @Test
  void дваУмножитьНаПять() {
    тестПосчитать(10, "2*5");
  }
}


It turns out that the second program automatically passes the tests for the first + even its own.

This completely suits me in everything, except that I don’t see the possibility of running tests one at a time without code duplication, only all in a bunch, if the class starts right away. Because only the Calculate test is redefined, and there is simply no button in IntellijIdea to run a single test.

Question : Is it possible with such a structure to run tests one at a time?

If yes, how is it done correctly? How can I at least compose a search query to google what I'm looking for? :)

If not (and in general): what would you advise to read / look at to solve the problem of running single tests when inheriting test classes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-12-05
@xez

Keep
It
Stupid
Simple

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question