Intro to java test frameworks

21
Test Automation 101 Intro to Java Test Frameworks

Transcript of Intro to java test frameworks

Test Automation 101Intro to Java Test Frameworks

Frank Amankwah
nice

Who am I?LimTestertwitter : lsim001www.codinglegends.com

What are we going to learn● Setting up a test project from scratch● How to write a test using JUnit● How to write a test using TestNG

Good habits● Consistent naming convensions of

tests● Atomic● Single assertion

Common terminology● Test suite● Test class● Test● Before and After test hooks

○ class○ test

● Assertions

Tools you need● Git● Maven● Java IDE (Eclipse or IntelliJ)

What to test against● The world famous calculator program● Get the code from Github

○ git clone [email protected]:limsim/intro-to-java-test-frameworks-start.git

Different ways of running test● From command line run mvn test● Use IDE ● (You could use Java and JUnit but the

command is long and not user friendly)

Test classpublic class CalculatorTest{

}

Testpublic class CalculatorTest{

@Testpublic void

addReturnsSumOfTwoNumbers() {

}}

TestAdd JUnit dependency to Maven <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>

Make an assertionpublic class CalculatorTest{

@Testpublic void

addReturnsSumOfTwoNumbers() {Assert.assertEquals("Fail!", 1,

new Calculator().add(1, 1));}

}

Common assertions● assertTrue● assertFalse● assertNotNull● assertSame● assertNotSame

ExcerciseTry writing tests to do the following● Use assertTrue to check that expected and

actual results from add() are the same.● Use assertFalse to check that expected and

actual results from add() are not the same.● Use assertNotNull to check that Calculator has

been created.

Generate HTML reportsNeed to add a reporting plugin<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.18.1</version> </plugin> </plugins> </reporting>

Generate HTML Report● Run mvn site● The HTML Report is generated in

target/site

Test hooks@Beforepublic void doThisFirst() {

}

@Afterpublic void doThisLast() {

}

TestNGAdd TestNG dependency <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> <scope>test</scope> </dependency> </dependencies>

TestNG● Create a test class● Create a test method● Write an assertion

TestNG hooks@BeforeClasspublic void createCalculator() {

}

TestNG hooks● AfterClass● Before(After)Method● Before(After)Suite● Before(After)Groups● Before(After)Test