简体   繁体   English

每次测试前运行方法

[英]Run method before each test

Im using testng 6.11 and writing tests in the following test class: 我正在使用testng 6.11并在以下测试类中编写测试:

public class MyTest{

    public int i;

    public void init(){
        //initialize i
    }

    @Test
    public void test1(){
        //test some
    }

    @Test
    public void test2(){
        //Here I need fresh value of i as it would be
        //right after invocation of init()
        //...
        //test something else
    }
}

Is it possible to make testng run init() method before invocation of each test in a test class? 是否有可能在调用测试类中的每个测试之前使testng运行init()方法?

Annotate init() with @BeforeMethod annotation. @BeforeMethod注解注释init() See http://testng.org/doc/documentation-main.html#annotations 参见http://testng.org/doc/documentation-main.html#annotations

Sure, your can use the annotation for that 当然,您可以为此使用注释

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. @BeforeTest:带注释的方法将在运行属于该标记内的类的任何测试方法之前运行。

You can use @BeforeMethod annotatation to execute any method before every test. 您可以使用@BeforeMethod注释在每次测试之前执行任何方法。

Example

暂无
暂无

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

相关问题 如何在每个@Test之前运行@BeforeClass方法 - How to run @BeforeClass method before each @Test Java / Selenium(ieDriver)-如何避免在每次测试运行中在方法(jUnit)之前重复webapp登录 - Java/Selenium (ieDriver) - how to avoid repeating the webapp login Before Method (jUnit) in each Test run 如何在每个JUnit @Test方法之前单独运行一些代码,而不使用@RunWith和AOP? - How to run some code before each JUnit @Test method individually, without using @RunWith nor AOP? 在每个请求之前运行资源方法 - Run resource method before each request 在JUnit中调用@Before批注之前,更改每个测试方法的值 - Change the value for each test method before @Before annotation is called in JUnit 每次在其他测试方法之前调用一个测试方法 - invoking a test method each time before other test methods 尝试在失败测试之前在before()方法上运行重试 - Trying to run retries on before() Method before failing a test BeforeTest不会在该类中的每个Test方法之前运行 <test> 标签 - BeforeTest would not run before every Test method of the class in <test> tag 如何在Spring中真正地单独运行每个集成测试,或者至少在每个测试之前运行所有命令行运行器? - How to run each integration test truly individually in Spring, or at the very least run all commandlinerunners before each test? 每种测试方法前后如何执行 sql 脚本 - How to execute sql script before and after each test method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM