(testng多个class文件执行时混乱,不是等一个class内的所有methods执行完再去执行下一个class内的内容)问题的解决...

问题描述如下:

 

We use TestNG and Selenium WebDriver to test our web application.

Now our problem is that we often have several tests that need to run in a certain order, e.g.:

  • login to application
  • enter some data
  • edit the data
  • check that it's displayed correctly

Now obviously these tests need to run in that precise order.

At the same time, we have many other tests which are totally independent from the list of tests above.

So we'd like to be able to somehow put tests into "groups" (not necessarily groups in the TestNG sense), and then run them such that:

  • tests inside one "group" always run together and in the same order
  • but different test "groups" as a whole can run in any order

The second point is important, because we want to avoid dependencies between tests in different groups (so different test "groups" can be used and developed independently).

Is there a way to achieve this using TestNG?

Solutions we tried

  • At first we just put tests that belong together into one class, and used dependsOnMethods to make them run in the right order. This used to work in TestNG V5, but in V6 TestNG will sometimes interleave tests from different classes (while respecting the ordering imposed by dependsOnMethods). There does not seem to be a way to tell TestNG "Always run tests from one class together".
  • We considered writing a method interceptor. However, this has the disadvantage that running tests from inside an IDE becomes more difficult (because directly invoking a test on a class would not use the interceptor). Also, tests using dependsOnMethods cannot be ordered by the interceptor, so we'd have to stop using that. We'd probably have to create our own annotation to specify ordering, and we'd like to use standard TestNG features as far as possible.
  • The TestNG docs propose using preserve-order to order tests. That looks promising, but only works if you list every test method separately, which seems redundant and hard to maintain.

Is there a better way to achieve this?

I am also open for any other suggestions on how to handle tests that build on each other, without having to impose a total order on all tests.

PS

alanning's answer points out that we could simply keep all tests independent by doing the necessary setup inside each test. That is in principle a good idea (and some tests do this), however sometimes we need to test a complete workflow, with each step depending on all previous steps (as in my example). To do that with "independent" tests would mean running the same multi-step setup over and over, and that would make our already slow tests even slower. Instead of three tests doing:

  • Test 1: login to application
  • Test 2: enter some data
  • Test 3: edit the data

we would get

  • Test 1: login to application
  • Test 2: login to application, enter some data
  • Test 3: login to application, enter some data, edit the data etc.

In addition to needlessly increasing testing time, this also feels unnatural - it should be possible to model a workflow as a series of tests.

If there's no other way, this is probably how we'll do it, but we are looking for a better solution, without repeating the same setup calls.

 

 

 

 

 

 

 

 

 

 

he situation can easily be reproduced using the code snippet

below.

I'd be really glad if the ordering could natively be turned to "per class
atomicity"

 


------------ ClassA.java --------------
package test;

import org.testng.annotations.Test;

public class ClassA {
        
   @Test
   public void testA1()
   {
      System.out.println ("testA1");
   }
   
   @Test
   public void testA2()
   {
      System.out.println ("testA2");
   }
   
   @Test(dependsOnMethods={"testA1","testA2"})
   public void testA3()
   {
      System.out.println ("testA3");
   }   
}

------------ ClassB.java --------------
package test;

import org.testng.annotations.Test;

public class ClassB {
        
   @Test
   public void testB1()
   {
      System.out.println ("testB1");
   }
   
   @Test
   public void testB2()
   {
      System.out.println ("testB2");
   }
   
   @Test
   public void testB3()
   {
      System.out.println ("testB3");
   }
}

------------ testng.xml --------------
<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM " http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" parallel="none">
  <test name="MyTest">    
    <classes>
      <class name="test.ClassA"/>
      <class name="test.ClassB"/>
    </classes>  
  </test>
</suite>

 

------ Output -------
[TestNG] Running:
 
/usr/users/arno/local_home/workspace/playground/src/test/testng_dependsOnOrdering.xml

testA1
testA2
testB1
testB3
testB2
testA3

I have like 30 test-classes each with 4 or 5 tests and have 
dependsOnMethod on them. testng.xml would be unmanagable if I have to 
add <test>  for each one of them.

 

 

 

 

 

 

 

目前来说的解决办法:
the test-runner _interleaves_ different tests from different classes!
Tests in the same <test> tag will always be run interleaved, if you want them separate, you should probably put them in different <test> tags...

 

 

不知道testng后续会不会优化,这个的确很annoying

 

转载于:https://www.cnblogs.com/melody-emma/p/4825958.html

可以通过使用JUnit或TestNG测试框架来实现一次执行多个用例,并获取每个执行用例后的值。 首先,创建一个测试类,并在该类中定义测试方法来执行需要测试的用例。在每个测试方法中,可以定义一些变量来存储执行用例后的值,并在测试方法中将其设置为实际的结果值。 例如,下面是一个简单的测试类,其中包含两个测试方法,用于执行两个用例,并将结果存储在变量中: ``` import org.junit.Test; public class MyTest { @Test public void testCase1() { int result = 2 + 2; System.out.println("Result of test case 1: " + result); } @Test public void testCase2() { String result = "Hello, World!"; System.out.println("Result of test case 2: " + result); } } ``` 然后,可以使用测试框架来运行这些测试方法,并获取每个测试方法的结果值。例如,使用JUnit框架,可以使用以下代码来运行测试方法并获取结果: ``` import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class MyTestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(MyTest.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } for (int i = 0; i < result.getRunCount(); i++) { System.out.println("Result of test case " + (i+1) + ": " + result.getRunCount().get(i).toString()); } } } ``` 在上面的代码中,首先使用JUnitCore类的runClasses()方法来运行测试类中的所有测试方法。然后,使用Result类的getFailures()方法来获取所有失败的测试方法。最后,使用Result类的getRunCount()方法来获取所有运行的测试方法,并使用toString()方法将它们的结果值转换为字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值