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 .

No comments:

Post a Comment