Saturday, August 10, 2019

XML to HTML report using xsl

XML to HTML report using xsl



Sample junit xml report

<testsuite errors="0" failures="2" hostname="Deepaks-MacBook-Air.local" name="xxx.scalaTests.common.DefaultColumnAdditionSpec" tests="4" time="7.66" timestamp="2019-08-10T09:58:56">

<testcase name="1To validate resultant df, when input datatype is of type string. 1To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="7.625"> </testcase>

<testcase name="3To validate resultant df, when input datatype is of type string. 3To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.003"> </testcase>

<testcase name="4To validate resultant df, when input datatype is of type string. 4To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.008">
<failure message="org.scalatest.exceptions.TestFailedException was thrown." type="class org.scalatest.exceptions.TestFailedException">...</failure>
</testcase>

<testcase name="5To validate resultant df, when input datatype is of type string. 5To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.0">
<failure message="org.scalatest.exceptions.TestFailedException was thrown." type="class org.scalatest.exceptions.TestFailedException">...</failure>
</testcase>

</testsuite>

----save as test.xsl--------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>Result</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Test Name</th>
      <th style="text-align:left">Result</th>
    </tr>
    <xsl:for-each select="testsuites /testsuite/testcase">
    <tr>
      <td><xsl:value-of select="@name"/></td>
<xsl:choose>
<xsl:when test="failure"><td>FAIL</td></xsl:when>
<xsl:otherwise><td>PASS</td></xsl:otherwise>
</xsl:choose>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------

STEPS:

  1. Save the above xsl code as test.xsl
  2. Open terminal
  3. cd to juit report file.
  4. Run below code 
    1. MAC : xsltproc -o result.html test.xsl junit_result.xml 
    2. Win :  msxsl sourcefile.xml test.xsl -o junit_result.html 









Sunday, August 4, 2019

ScalaTest : testng

ScalaTest : TestNG


Pre-Req :
  • Os - Linux ubuntu / debian
  • Java /jdk 1.8 +
  • scala 2.11 +
  • Intellij
  • Install sbt plugin in Intelli
  • Create an scala project in Intelli using sbt

Commands:
scala -version
sbt sbtVersion

----build.sbt (add below line)--------
scalaVersion := "x.xx.0"
libraryDependencies += "org.scalatest" % "scalatest_x.xx" % "3.0.8" % "test"
libraryDependencies += "org.testng" % "testng" % "6.10"

--- install sbt----
open terminal
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt
---------------------

Location : src/test/scala/ExampleTestNGSuite.scala

//https://searchcode.com/codesearch/view/16471591/
//http://doc.scalatest.org/1.8/index.html#package

import org.scalatestplus.testng.TestNGSuite
import org.testng.annotations.Test
import org.testng.annotations.BeforeMethod
import org.testng.annotations.BeforeClass
import org.testng.annotations.BeforeSuite
import org.testng.annotations.AfterMethod
import org.testng.annotations.AfterClass
import org.testng.annotations.AfterSuite
import org.testng.annotations.DataProvider

class ExampleTestNGSuite extends TestNGSuite {

// @AfterSuite def failAfterSuite(){ throw new Exception("fail in before method") }
@BeforeMethod def passBeforeMethod(){println("b3")}
@BeforeClass def passBeforeClass(){println("b2")}
@BeforeSuite def passBeforeSuite(){println("b1")}
@AfterMethod def passAfterMethod(){println("a2")}
@AfterClass def passAfterClass(){println("a3")}
@AfterSuite def passAfterSuite(){println("a1")}
@Test(invocationCount = 10)
def thisTestRunsTenTimes = {}
/*
@Test(groups = Array("runMe"))
def testWithException(){
throw new Exception("exception!!!")
}
@Test(groups = Array("runMe"))
def testWithAssertFail = assert( 1 === 2, "assert fail!!!" )

@Test(dependsOnMethods = Array("testWithException"))
def testToGetSkipped = {}
*/
@DataProvider(name = "any_name")
def andValues = {
val test_var = Array("0", "1")
for( x <- test_var; y <- test_var ) yield Array(x,y)
}

@Test(dataProvider = "any_name")
def testAndStates(a: String, b: String){
println("a="+ a + ",b="+ b)
}
}

-----Run Sbt in terminal -----
cd to projects >
run command "sbt test"
------------------------


Result :
A testNG report has to be generated in the project folder .