Test Automation 101
Intro to Java Test Frameworks
Who am I?
Lim
Tester
twitter : lsim001
www.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 git@github.com: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 class
public class CalculatorTest
{
}
Test
public class CalculatorTest
{
@Test
public void addReturnsSumOfTwoNumbers()
{
}
}
Test
Add JUnit dependency to Maven
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Make an assertion
public class CalculatorTest
{
@Test
public void addReturnsSumOfTwoNumbers()
{
Assert.assertEquals("Fail!", 1,
new Calculator().add(1, 1));
}
}
Common assertions
● assertTrue
● assertFalse
● assertNotNull
● assertSame
● assertNotSame
Excercise
Try 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 reports
Need 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
@Before
public void doThisFirst() {
}
@After
public void doThisLast() {
}
TestNG
Add 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
@BeforeClass
public void createCalculator() {
}
TestNG hooks
● AfterClass
● Before(After)Method
● Before(After)Suite
● Before(After)Groups
● Before(After)Test

Intro to java test frameworks

Editor's Notes

  • #2 Set up prerequisites Maven Create github project and split presentation into lessons Lesson 1 - create test class Lesson 2 - create test method Lesson 3 - write assertion Lesson 4 - write another test using a different assertion Lesson 5 - Before hook Lesson 6 - After hook
  • #5 Atomic? What does that mean? Nothing to do with bombs. Hmm...assertion? Basically checking.
  • #9 Running the tests in the IDE won’t generate test reports but it’s more convinient when you’re writing tests or want to just run a single test. Let’s try running “mvn test” (before we write any test). What do we learn from the output?
  • #12 Try running “mvn test”again What do you see this time?
  • #14 Try some of the other assertions
  • #22 @BeforeSuite: The annotated method will be run before all tests in this suite have run. @AfterSuite: The annotated method will be run after all tests in this suite have run. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod: The annotated method will be run before each test method. @AfterMethod: The annotated method will be run after each test method.