首页 文章

TestNG框架和新关键字

提问于
浏览
0

我是TESTNG框架的新手 . 在这个框架中没有主要方法 . 因此我怀疑,我们可以使用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>

1 回答

  • 0

    您可以使用注释:

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

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

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

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

    因此,在您的测试类中,创建一个方法,例如 setUp 创建 objIdeal 的实例

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

相关问题