Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you mix TestNG and JUnit Assertions together within the same Test / Framework?

Can you mix TestNG and JUnit Assertions together within the same Test / Framework?

For example lets say I have developed a framework, is it possible to have the following:

  • A TestNG class which uses JUNit Assertions
  • A TestNG which uses both Junit and TestNG assertions?

Thanks For your help

like image 774
Gbru Avatar asked Feb 05 '23 12:02

Gbru


2 Answers

Keep in mind what the essence of both frameworks is: they execute testcases.

Of course you can put whatever annotations you want into your code. But the point is: those annotations by themselves mean nothing. They are just markers on classes/method definitions.

It is the underlying framework, that checks for their presence at runtime and does "something" when this or that annotation is found.

Meaning: in order for JUnit resp. TestNG annotations to cause the desired effects at runtime ... you have to run the test either within the JUnit or the TestNG framework. But - you can't run both frameworks in parallel, in the same JVM, on the same source code.

In other words: if there is a need to do so, then you have to separate your testcases into different buckets: one for JUnit tests; one for TestNG tests. But forget about mixing things on a lower level!

And even when it would be technically possible to run both frameworks "together" in one JVM - you still do not want to do that. Simply because those frameworks are not designed to support this requirement; and because nobody else does it; and therefore your chances of hitting all kinds of obvious and subtle bugs is way too high!

But I have to admit: I misread the question. If its solely about dealing with those asserts, then I agree to Grasshoper's comment to this answer; I do not see a hard technical reason for that to not work.

like image 192
GhostCat Avatar answered Apr 30 '23 04:04

GhostCat


You can use any assertion library you want in your tests: TestNG, JUnit, AssertJ, Hamcrest, ...

It is possible to run JUnit tests with TestNG too because TestNG supports JUnit if you configure it well.

like image 43
juherr Avatar answered Apr 30 '23 04:04

juherr