Try running JUnit tests that don't actually execute the statement

My team is working on training some of our developers to test. They understand why they should write tests on board, that they should write tests, but they are a little behind in writing good tests.

I just saw a commit like this

public void SomeTest{ @Test public void testSomething{ System.out.println(new mySomething.getData()); } 

Thus, they at least made sure that their code gave them the expected result by looking.

There will be a bit before we can really sell the idea of ​​code reviews. At the same time, I was considering the possibility of JUnit abandoning any tests that do not have actual assertXXX or fail statements. I would like this error message to say something like "Your tests should use statements and actually check the output!".

I fully expect this to result in calls like assertTrue(1 == 1); . We are working on the purchase of a team for proper testing and code reviews, are there any technical mechanisms that we can use to make life easier for developers who have already received this? What about the technical mechanisms that will help new guys understand?

+4
source share
3 answers

You can use some static code analyzer.

I use PMD , which includes the JUnit rule set . There are many IDE plugins that will flag policy violations in the IDE. You can customize a set of rules to suit your needs.

You will also profit from other sets of rules that will warn you about violations of the code style / best practice (although sometimes you have to choose if the tool or you are a fool :-)).

+1
source

I think you should consider organizational changes: mentoring, training, code reviews.

Tools can help you if you use them in good faith with a basic understanding of goals. If one of them is missing, they will not help you.

People are just smart to dump or work with metrics. I think your assessment is incorrect, that β€œthey” are on board if they cannot write one useful test. At this stage, automatic tools are simply not the right tools. You cannot learn by telling the program what to do next.

+2
source

answer the question for future viewers.

JUnit uses reflection to run the function under test if there are any Exception errors, Error throws β†’ failed, otherwise it will succeed. The Assert class is just the utils class.

0
source

Source: https://habr.com/ru/post/1341213/


All Articles