Updated March 29, 2023
Definition of JUnit XML
JUnit xml is a framework that was used in many applications in the test frameworks. By default, the test will generate xml files which are simple reports used for the execution of the test. These files are used to generate the report which was custom, we can also generate the reports as per the requirement of testing. By using this we can generate the html reports by using the xml files.
What is JUnit XML?
- Junit testing will by default create the xml reports for the test execution. We are using xml reports after generating the junit of html report.
- Junit testing framework is introduced in the file format of xml which was used in the execution of the test suite.
- For the testing purpose, junit will support the different types of formats which are xml and other. Using junit will transform each of the formats into the result of xml. As we know that junit has test suites. Basically, the suite is the way for aggregating the test together as per results.
Using JUnit XML
- By using junit we can export or review our results using the format. We can also download the schema. Basically, the junit framework is using xml format files to report on the execution of the test suite.
- The xml files will be processed by the programs like junit plugin or Jenkins to the display result of the tests.
- The webpage is providing the sample xml file which is describing all valid elements which are used by the plugin of the junit Jenkins.
- It also serves as a template for other programs which serves different programs which process into the different styles which xml file like as plugin of maven surefire.
- We need to create the project we are creating the spring boot project.
- We need to create a java class for the testing purpose in the below example we are creating a java class name as junittest.
Code –
public class junittest {
@Test
public void testMethodOne () {
Assert.assertTrue (true);
}
}
- After creating the java class now we are creating the xml file into the src folder. We are creating the xml file as follows. In xml file, we need to provide the class name which we have created in the above example. Also, we need to give the test name which we have executing.
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<suite name = "Junit Suite">
<test name = "Junit xml test">
<classes>
<class name = "junittest" />
</classes>
</test>
</suite>
- After creating xml files then we are running the project by using java application are as follows.
- We can also use the file format in python. Python is creating the junit tests which are read by the tools like bamboo or Jenkins. Suppose we are working on creating a test suite that was written in python and needs to take the advantage of bamboo and Jenkins tools for the capability of reporting is very useful.
- To use in python we need to install the same by using the pip command.
pip install junit-xml
- After installing this plugin we are using this module in our program. We are importing the module by using the import keyword.
- The below example shows how to import the module into our program. In the below example we are importing the test suite and test case module by using the package.
from junit_xml import TestSuite, TestCase
- The below example shows how to use in our code as follows.
Code –
from junit_xml import TestSuite, TestCase
junitxml = [TestCase('Junit', 'some.class.name', 456.654, 'stud name is ABC', 'stud name is PQR')]
junit = TestSuite("my test suite", junitxml)
print (TestSuite.to_xml_string([junit]))
JUnit XML Output
- To display the output we need to create the java project. The below example shows creating projects are as follows. We are creating the project name as JunitXML.
- In this step, we are creating the project template of JunitXML in spring boot. We have provided project group name as com.example, artifact name as JunitXML, project name as JunitXML, and selected java version as 11. We are defining the version of spring boot as 2.6.7.
Group – com.example
Artifact name – JunitXML
Name – JunitXML
Spring boot – 2.6.7
Project – Maven
Java – 11
Package name - com.example.JunitXML
Project Description - Project for JunitXML
- In this step, we are extracting the downloaded project and opening the same by using the spring tool suite.
- In this step, we are checking all project structures and their files. Also, we are checking the pom.xml file is created or not. Suppose this file is not created then we need to create the same manually. In the below example this file is created, so we have no need to create it manually.
- To generate the output we need to create a java file we are creating it below java files are as follows.
Code –
public class junittest {
@Test
public void test1 () {
Assert.assertTrue (true);
}
@Test
public void test2 () {
Assert.assertTrue (false);
}
}
- After creating the java class file now we are creating the xml file are as follows
Code –
<?xml version = "1.0" encoding = "UTF-8"?>
<suite name = "Junit Suite">
<test name = "Junit xml test">
<classes>
<class name = "junittest" />
</classes>
</test>
</suite>
- After creating the xml files now we are running the application and checking the output as follows.
- After running the project now we are running the test suite by using junit as follows.
Conclusion
Junit testing is by default creating the xml reports for the test execution. We are using xml reports after generating the junit of html report. It is a framework that was used in many applications in the test framework. By default, the test will generate xml files.
Recommended Articles
This is a guide to JUnit XML. Here we discuss the Definition, What is JUnit XML, how to use it, and examples with code implementation. You may also have a look at the following articles to learn more –