问题
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
回答1:
I think the short answer is no. If you notice when you mark a method as a test with the @Test annotation you are prompted which framework to import. Same thing witht the assertions as well. Think about it, a Testng test wouldn't be able to 'record' a result if prompted to use a Junit assertion. Hence, when writing a test with a Testng @Test annotation; the Assertion automatically defaults to a Testng assertion.
Update, after OP's comments:
Gbru check your \projectFolder\test-output\junitreports the xml you are looking for might already be there..
Taken from: testNG doc Junit reports
6.2.3 - JUnitReports
TestNG contains a listener that takes the TestNG results and outputs an XML file that can then be fed to JUnitReport. Here is an example, and the ant task to create this report:
<target name="reports">
<junitreport todir="test-report">
<fileset dir="test-output">
<include name="*/*.xml"/>
</fileset>
<report format="noframes" todir="test-report"/>
</junitreport>
</target>
回答2:
Answer is NO
. Either you can use TestNG or JUnit at a time. You can't integrate both at a time.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/43757487/can-you-mix-testng-and-junit-assertions-together-within-the-same-test-framewor