【发布时间】: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>
【问题讨论】:
-
您需要使用专用的运行器类运行测试类;目前还不清楚你的环境是什么