简体   繁体   English

TestNG 中的继承和@BeforeSuite

[英]Inheritence and @BeforeSuite in TestNG

I have been working on TestNG and there was a problem associated with some of my code.我一直在研究 TestNG,但我的一些代码出现了问题。 Here's the code:这是代码:

public class Main {
public String baseurl ;
public WebDriver webdriver ;
protected Main(){

    //baseurl = "http://goodreads.com";
    //webdriver = new FirefoxDriver();

}

@BeforeSuite
public void setup(){

    baseurl = "http://goodreads.com";
    webdriver = new FirefoxDriver();
}

Above code is of the base class上面的代码是基类的

@Test
public void do_login(){

    super.webdriver.get(super.baseurl);

This is of inherited class这是继承的类

Now as far as I know the BeforeSuite should executed first and the values of baseurl and webdriver should get initialized to the specified values.现在据我所知,BeforeSuite 应该首先执行,并且 baseurl 和 webdriver 的值应该被初始化为指定的值。 But I'm getting a NullPointerException in the above code.但是我在上面的代码中得到了一个 NullPointerException。 The problem is resolved when I assign the variables as static or if I initialize the variables in the constructor.当我将变量分配为静态变量或在构造函数中初始化变量时,问题就解决了。 But why are they not being initialized when I put them in the method(which is supposed to execute before the @Test executes anyway)?但是为什么当我将它们放入方法中时它们没有被初始化(无论如何应该在 @Test 执行之前执行)? Is there some concept about java that I'm missing?是否有一些我遗漏的关于 java 的概念? Please explain请解释

You don't need to include both classes to the suite.您不需要将这两个类都包含到套件中。 Only @Test containing class should be included.只应包含包含类的 @Test。 I think that's the problem.我认为这就是问题所在。 I also believe that @BeforeSuite annotated method will be executed not depending on the way you run your tests (as TestNG method or TestNG suite), since default suite will be created in order to run tests anyway.我也相信 @BeforeSuite 注释方法的执行不取决于您运行测试的方式(作为 TestNG 方法或 TestNG 套件),因为无论如何都会创建默认套件以运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM