简体   繁体   English

Java - JUnit - 在子 Class 中执行 @Test 注释

[英]Java - JUnit - Execute @Test Annotations in Child Class

Parent Class:父 Class:

@BeforeTest
public void prepareTest()

@Test
public void runTest()

@AfterMethod
public void tearDown()

@AfterTest
public void endReport()

Test case classes directory:测试用例类目录:

  • TestFolder测试文件夹

    --> TestCase1 --> 测试用例1

    --> TestCase2 --> 测试用例2

    --> TestCase3 --> 测试用例3

Within each of these test case class files, I have varying amount of methods, obviously depending on the test case.在每个测试用例 class 文件中,我有不同数量的方法,显然取决于测试用例。 But in order to execute these methods, I had to create a method in every test case called executeTest .但是为了执行这些方法,我必须在每个测试用例中创建一个名为executeTest的方法。 Within this method, I call all the methods in that class file in the order I need them.在这个方法中,我按照我需要的顺序调用 class 文件中的所有方法。

public boolean executeTest()
{
  myMethod1()
  myMethod2()
  myMethod3()
  myMethod4()

  return true;
}

Then, inside of my runTest method in my parent class , I have a method call to invoke the executeTest method within the child class.然后,在我的父 class 的 runTest 方法中,我有一个方法调用来调用子 class 中的executeTest方法。

This all works perfectly fine, but what I am wanting to do is get away from the method in the child classes and instead assign the @Test annotation to each method, with a priority for the the order of execution.这一切都很好,但我想要做的是远离子类中的方法,而是将@Test注释分配给每个方法,并优先考虑执行顺序。

Something like this:像这样的东西:

@Test(priority = 1)
myMethod1()

@Test(priority = 2)
myMethod2()

@Test(priority = 3)
myMethod3()

@Test(priority = 4)
myMethod4()

Is this possible to accomplish?这有可能实现吗? Is there a better way of doing this perhaps?有没有更好的方法来做到这一点?

Edit:编辑:

Here is the function I use to invoke the method executeTest in the child classes:这是我用来在子类中调用方法 executeTest 的 function:

public boolean executeMethod(String className)
    {
        //https://stackoverflow.com/questions/18778819/dynamically-calling-a-class-method-in-java
        //https://stackoverflow.com/questions/5266532/can-i-get-all-methods-of-a-class
        
        //String methodNames[] = new String[]{"executeTest"};
        String mName = "";
        
        try {

            Class classRef = Class.forName(className);
            Object instance = classRef.newInstance();
            
            Method[] methodNames = classRef.getDeclaredMethods();

            for (Method methodName : methodNames)
            {
                mName = methodName.getName();
                
                try
                {
                    if(mName.equalsIgnoreCase("executeTest"))
                    {
                        Method method = classRef.getDeclaredMethod(mName);
                        method.invoke(instance);
                    }
                }
                catch (NoSuchMethodException | SecurityException | IllegalArgumentException ex)
                {
                    ex.printStackTrace();
                }
                catch(InvocationTargetException e)
                {
                    if(e.getCause().toString().toLowerCase().contains("TestException".toLowerCase()))
                    {
                        throw new TestException();
                    }
                }
            }
        }
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex)
        {
            ex.printStackTrace();
        }
        
        return true;
    }

In my parent class file, within the runTest method, I call it like this:在我的父 class 文件中,在 runTest 方法中,我这样称呼它:

executeMethod(projectName + "." + TestCase);

You can use @Order annotation to set the priority in JUnit5.您可以使用@Order注解在 JUnit5 中设置优先级。

@TestMethodOrder(OrderAnnotation.class)
public class JUnit5TestOrder { 
 
        @Test
        @Order(1)
        public void Testcase_3() {
                System.out.println("Testcase_3 executes");
        }
 
        @Test
        @Order(2)     
        public void Testcase_1() {
                System.out.println("Testcase_1 executes");
        }
      
        @Test
        @Order(3)
        public void Testcase_2() {
                System.out.println("Testcase_2 executes ");     
        } 
}

And in JUnit4 you can use @FixedMethodOrder to order the test cases.JUnit4中,您可以使用@FixedMethodOrder对测试用例进行排序。 For more details check out JUnit Test Execution Order .有关更多详细信息,请查看JUnit 测试执行顺序

@FixMethodOrder(MethodSorters.DEFAULT)
public class JUnit4TestOrder { 
@Test
        public void Testcase_3() {
                System.out.println("Testcase_3 executes");
        }
     @Test
     public void Testcase_1() {
                System.out.println("Testcase_1 executes");
        }
     @Test
      public void Testcase_2() {
                System.out.println("Testcase_2 executes ");     
} }

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

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