I have multiple testNG test classes that extend a Base Test class and all use the same common objects. I want to have the object creation done automatically in BaseTest so I don't have to include it in each test class. As of now, the code only works if I add createPages() to the start of the test. I tried putting them in the BaseTest class using #BeforeClass and #BeforeSuite but both gave a null pointer exception meaning they weren't instantiated before the #Test test123 was run I beleive.
public someTest extends BaseTest {
#Test
public void test123(){
createPages(); //i want to be able to remove this and have it done in BaseTest
menuPage.scroll();
}
}
public BaseTest {
MenuPage menuPage;
public void createPages() {
menuPage = new MenuPage(getDriver());
}
/*
#BeforeSuite
public void beforeSuite() {
createPages();
}
#BeforeClass
public void beforeClass() {
createPages();
}
*/
}
#beforeTest is one such annotation. A method with #beforeTest annotation will run, before any test method belonging to the classes inside the test tag
inside ur BaseTest
public class Basetest{
#BeforeTest
public void doBeforeTest() {
createPages();
}
}
Related
How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.
I have 3 class . Test1,Test2 and Base class. Test1 and Test2 extends Base class. When i am executing both classes using TestNg xml it fails #BeforeClass with null pointer exception. Report of test1 is getting generated. Also without extent report its working fine. Please help me
====================================Base Class==============================
public class Base
{
public ExtentReports reports;
public ExtentTest logger;
#BeforeClass
public void startBrowser()
{
String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
logger = reports.createTest("Test1"+name);
System.out.println("Started Test1");
}
#BeforeSuite
public void initialSetup()
{
String name= LocalDateTime.now().format(DateTimeFormatter.ofPattern("ddMMMYY_HHmmss"));
ExtentHtmlReporter extent = new ExtentHtmlReporter(new File("./Reports/"+name+".html"));
reports = new ExtentReports();
reports.attachReporter(extent);
}
#AfterClass
public void end()
{
reports.flush();
}
}
===========================Test1 class=====================
public class Test1 extends Base{
#Test
public void TestCase()
{
logger.info("Test1");
}
}
====================Test2 class=============================
public class Test2 extends Base{
#Test
public void f()
{
System.out.println("Started Test2");
}
}
The problem lies in your test code.
You have initialised the data member reports in a #BeforeSuite annotated method.
This gets executed only once per <suite> tag.
Since both your classes are extending from the same base class, the initialSetup() method will get executed only once (for e.g., Test1.java).
That is why when your #BeforeClass annotated startBrowser() attempts at accessing reports.createTest("Test1"+name); for Test2 you hit a NullPointerException.
You need to change this by moving your initialSetup() invocation from within a #BeforeClass method.
I am writing automation testing script for a project . Here , I will have many test classes for which I will be using same #BeforeTest Method . I tried by creating a base class (in which I declared before test method ) and extending them in my test classes . But its not working . Is there any other way to have a common beforeTest method in a seperate java file and use it for all my test classes .
Using a base class for my other classes works for me. You should use #BeforeMethod for your needs since #BeforeTest is used to run any #Test included in the <test> tag in a testNG.xml file.
public class BaseClass {
#BeforeMethod
public void before() {
System.out.println("Before method");
}
}
and then
public class ATestClass extends BaseClass {
#Test
public void testOne() {
System.out.println("testOne run");
}
#Test
public void testTwo() {
System.out.println("testTwo run");
}
}
gave me the result
Try it out!
If you are using PowerMockito or Mockito you can achieve by following.
public abstract class ParentClass{
#Before
public void init() {
System.out.println("inniinii");
}
}
#RunWith(PowerMockRunner.class)
public class ChildClass extends ParentClass{
#Test
public void test() {
System.out.println("hello test");
}
}
I have a little problem with testNG. Below my example code:
abstract class parent {
abstract void beforeTest();
#Test
void test() {
// some testing
}
}
class child extends parent {
#BeforeTest
void beforeTest() {
\\some before things
}
}
And the question is how to do this code work properly? So I want to perform beforeTest() method and if it fails the test method shoud skip. How can I achieve this thing?
Usually, the configuration methods go into the parrent and test classes should extend the parrent.
So, try using this example for your tests:
abstract class TestBase {
#BeforeTest
public void beforeTest() {
// do config here
// this will run for each of you <test> tag in your testng.xml suite
}
#BeforeMethod
public void beforeMethod() {
// do some config here
// this will run for each method annotated with #Test
}
}
class SomeTestClass extends TestBase {
#Test
public void some_test() {
// some testing
}
}
Here's my code below, testSample() gets executed successfully. Please suggest what could possibly be wrong
class DataServiceTest extends GrailsUnitTestCase{
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
You can't extend a TestCase and use annotations at the same time. If you want to create a test suite with annotations, you can use #RunWith annotation:
#RunWith(Suite.class)
#Suite.SuiteClasses({ DataServiceTest.class, OtherTest.class })
public class AllTests {
// empty
}
public class DataServiceTest { // no extends here
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
Another option using JUnit could be annotating the method with #Before and removing extends GrailsUnitTestCase from the class.