简体   繁体   English

@BeforeClass注释:Junit vs TestNG

[英]@BeforeClass annotation: Junit vs TestNG

Why the @BeforeClass method in JUnit is static whereas in TestNG its non-static ? 为什么JUnit@BeforeClass方法是static而在TestNGnon-static TestNG was developed as an improvement over JUnit , so why did they choose this way of implementation? TestNG是作为对JUnit的改进而开发的,那么他们为什么选择这种实现方式呢?

Since @BeforeClass runs only once, so making it static makes more sense than making it non static. 因为@BeforeClass只运行一次,所以使它static比使它非静态更有意义。 Also in TestNG, on which instance the @BeforeClass method is called? 同样在TestNG中,调用@BeforeClass方法的实例? Can someone cite an example for better understanding? 有人能引用一个例子来更好地理解吗?

The main difference between JUnit and TestNG is the test class instantiation. JUnit和TestNG之间的主要区别是测试类实例化。 JUnit always creates a new instance of the test class for each test method run. JUnit总是为每个测试方法运行创建一个新的测试类实例。 TestNG only creates one test class instance and then runs all test methods of this instance. TestNG只创建一个测试类实例,然后运行此实例的所有测试方法。

The JUnit approach guarantees the independency of all test methods. JUnit方法保证了所有测试方法的独立性。 It just does not matter, in which order they run. 它无关紧要,它们以何种顺序运行。 Additionally, all instance fields are always setup the same for each test method. 此外,对于每种测试方法,所有实例字段的设置始终相同。 Initializing data, that is common for all test methods, must take place at the class level, thus it must be static. 初始化数据(所有测试方法都很常见)必须在类级别进行,因此必须是静态的。 This is the reason, why the @BeforeClass method must be static. 这就是为什么@BeforeClass方法必须是静态的原因。

The TestNG approch does not guarante the independency. TestNG approch不保证独立性。 In fact, you cannot use an instance field in the same manner as in JUnit tests. 实际上,您不能以与JUnit测试中相同的方式使用实例字段。 If you change such a field in one test method, the changed value is still observabl in another test method. 如果在一个测试方法中更改了这样的字段,则更改的值仍然在另一个测试方法中是observabl。 However, this behavior has also an advantage: Sometimes there are dependencies between some test methods. 但是,这种行为也有一个优点:有时候某些测试方法之间存在依赖关系。 With TestNG, a tester can express them. 使用TestNG,测试人员可以表达它们。

Because of the one-instance-approach of TestNG, the @BeforeClass setup can also be a non-static method, but it is still run only once. 由于TestNG的单实例方法,@ BeforeClass设置也可以是非静态方法,但它仍然只运行一次。 It was a design decision, but testers using TestNG must be aware of that. 这是一个设计决策,但使用TestNG的测试人员必须意识到这一点。

Making a method static or non-static has nothing to do with being able to invoke that method only once at the beginning. 使方法静态或非静态与只能在开始时调用该方法无关。 You can call a static method as many times as you want. 您可以根据需要多次调用静态方法。 You can call a non-static method exactly one. 您可以将一个非静态方法调用一个。 There is no necessary correlation between the two : being static and invoking once. 两者之间没有必要的相关性:静态和调用一次。 At least, I am not aware of any direct consequence of making a method static which enables it to be invoked exactly once. 至少,我不知道使方法静态的任何直接后果,使其能够被调用一次。 static is rightly associated with single class, but not with single invocation. static与单个类正确关联,但不与单个调用关联。

Making a method static prevents it from accessing non-static members of the class. 使方法静态可防止它访问类的非静态成员。 But with having a non-static @BeforeClass method, you can also access non-static members, there by giving you more scope to access class variables. 但是,通过使用非静态@BeforeClass方法,您还可以访问非静态成员,为您提供更多访问类变量的范围。 Perhaps this is the reason why testng removed the restriction of @BeforeClass method being static. 也许这就是为什么testng删除@BeforeClass方法的静态限制的原因。

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

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