Junit Notes:
- Junit and TestNG are testing framework.
- Junit is not meant for Selenium but for selenium we could use it
- Junit is Jar file, inorder to use Junit we need to just download the jar file and give the build path in eclipse else right click import jar file when you get the error. Like Add Junit 4 library to the build path and execute by right clicking and then click Run As->Junit test
- Annotations help you control the execution of script in Junit
- When you are using Junit framework, one need not mention main method???
- Annotations list:
@Test --- Test Case. If you mention @Test in the front of a class then it means that class would be execute as a test case(test case would be created in framework).
@Before
@After
@Beforeclass
@Afterclass
@Ignore
@RunWith(Suite.class) --- helps to Run the classes in batch
@SuiteClasses( ------- batch in classes
{class1},
{class2})
@Rule
- In @Test, if you mention word 'Test' in method name then @Test annotations script would be executed in an order
- @After and @Before - are tagged to @Test. @After and @Before gets executed for everywhere @Test. To avoid this we have
- Assume.assumeTrue(<True>) ---
- Assertions
Assert.assertEquals(Expected, Actual)
- Drawback on Assertions(above statement) if Assertions fails then rest of the script will not be executed so we use try and catch.
Like:
try{
Assert.assertEquals(var1,var2)
}catch(Throwable t){
system.out.println(
- JUnit - Java Unit ---- as we use Java language. Different languages have different units.
- Csharp -- we use Test
- JUnit ---- has annotations, thats why we use units.
Source: http://www.tutorialspoint.com/junit/junit_overview.htm
Features
-
JUnit is an open source framework which is used for writing & running
tests.
-
Provides Annotation to identify the test methods.
-
Provides Assertions for testing expected results.
-
Provides Test runners for running tests.
-
JUnit tests allow you to write code faster which increasing quality
-
JUnit is elegantly simple. It is less complex & takes less time.
-
JUnit tests can be run automatically and they check their own results and
provide immediate feedback. There's no need to manually comb through a report of
test results.
-
JUnit tests can be organized into test suites containing test cases and even
other test suites.
-
Junit shows test progress in a bar that is green if test is going fine and it
turns red when a test fails.
It will just follow annoations instaed of going throug main method, ordering of executing the code could be changed using annoations(JUnit).
Annotation most freq used for testing are:
@Before -- method that is marked @Before will be executed before executing every test in the class.
@After
@Test
@BeforeClass
@AfterClass
- To create JUnit test case:
Go Eclipse IDE-> right click on your package -> JUnit test case
Provide class name(JUnitTest), you will have checkboxes for selecting annotations-- select setup() and teardown()
Once you are done with the test case, here is how you better mention before and after as below:
public class JUnitTest {
@Before
public void setUp() {
System.out.Println("first");
}
@After
public void tearDown() {
System.out.Println("third");
}
@Test
public void test1() {
System.out.Println("second1");
}
}
@Test
public void test2() {
System.out.Println("second2");
}
}
output:
first
second1
third
first
second2
third
- See JUnit will help you to create see test case in the results.
Assertion - is to indicate whether the particular statement returning true or false.
-
assertEquals("<actualvalue>", "<expected>"); --- compares actuals with expected, and produce true or false in the results. ---- you need not seperately write code to validate
assertEquals("Google",driver.getTitle());
-
assertTrue()
sub methods used with assertTrue() are
isEnabled()
isDisabled()
isSelected()
-
What are the packages that needs to be included???
-