【问题标题】:TestNG Framework and new KeywordTestNG 框架和新关键字
【发布时间】:2014-12-21 19:58:55
【问题描述】:

我是 TESTNG 框架的新手。在这个框架中没有 main 方法。因此我的疑问是,我们可以使用 new 关键字在测试套件的任何地方创建对象吗?或者是否有一个基本规则,对象只能在某些地方实例化(比如内部,@beforesuite、@test 等 testng 注释)?

在下面的代码中,当我在类中使用 new 关键字 (Ideal objIdeal = new Ideal();) 时它失败了,但是当我将它放在 @test 注释方法中时(登录或注销),它通过了。因此,在使用 Testng 框架时,在类中实例化对象的基本经验法则是什么。

package testing.ideal;

public class Application {

    public String strURL;

    public Application() {
        this.strURL = Ideal.strURL;
        System.out.println("the url is--" + this.strURL);
    }

}

package testing.ideal;
        import org.testng.annotations.*;

public class Ideal
        extends Application {
    public static String strURL = "XXX";
    Ideal objIdeal = new Ideal();

    @Test
    public void login() {
        System.out.println("This is an testng Method");
    }

    @Test
    public void logout() {
        System.out.println("This is an testng Method");
    }

}

/* This is Testng XML*/
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel = "none">
    <test name="login" >
        <classes>
            <class name="testing.ideal.Ideal">
            </class>
        </classes>
    </test>
</suite>

【问题讨论】:

  • 您需要使用专用的运行器类运行测试类;目前还不清楚你的环境是什么

标签: java testng


【解决方案1】:

您可以使用注释:

@BeforeSuite/@AfterSuite 在特定套件中的所有测试将运行之前/之后运行一些逻辑。

@BeforeMethod/@AfterMethod 在每个测试方法之前/之后运行一些逻辑。

@BeforeGroups\@AfterGroups 在属于该组的第一个/最后一个测试方法之前/之后运行一些

@BeforeTest\@AfterTest&lt;test&gt; 中包含的任何测试方法之前/之后运行一些逻辑

所以在你的测试类中,为例如创建一个方法。 setUp 创建objIdeal 的实例

 @BeforeTest
  public void setUp()
  {
     objIdeal = new Ideal();  
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多