testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

multiple thread run, the id of different beforeMethod is same #2190

Open theric opened 5 years ago

theric commented 5 years ago

TestNG Version

7.0.0

Expected behavior

id of different beforeMethod is not same

Actual behavior

id of different beforeMethod is same

Is the issue reproductible on runner?

IntelliJ

Test case sample

implements IReporter interface, override generateReport method, run test cases(at least two cases) by xml,set parallel="methods" ,check id of SuiteRunner->invokedMethods->TestNGMethod->beforeMethod

Test class:

package user.zdx.thread;

import common.ReportListener;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(ReportListener.class)
public class TestClassOne extends TestClassBase{

    @Test(description = "testOne",groups = "default")
    public void testOne(){
    }

    @Test(description = "testTwo",groups = "default")
    public void testTwo(){
    }

    @Test(description = "testThree",groups = "default")
    public void testThree(){
    }
}

test father class : TestClassBase

package user.zdx.thread;

import org.testng.annotations.BeforeMethod;
import java.lang.reflect.Method;

public class TestClassBase {

    @BeforeMethod
    public void setUp(Method method){
        System.out.println("before method thread name: "+Thread.currentThread().getName()+"----test method name----"+method.getName());

    }
}

custom report class:

package common;

import org.testng.IInvokedMethod;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.xml.XmlSuite;

import java.util.List;

public class ReportListener implements IReporter {

    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        for (ISuite suite : suites) {
            List<IInvokedMethod>  allInvokedMethods = suite.getAllInvokedMethods();
            for(IInvokedMethod iInvokedMethod:allInvokedMethods){
                String methodName = iInvokedMethod.getTestMethod().getMethodName();
                if("setUp".equals(methodName)){
                    System.out.println("before method id: "+iInvokedMethod.getTestMethod().getId());
                }
            }

        }
    }

}

xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" parallel="methods" thread-count="3">
    <parameter name="debug" value="true"></parameter>
    <test name="testclass">
        <classes>
            <class name="user.zdx.thread.TestClassOne">
            </class>
        </classes>
    </test>
</suite>

run xml output:

before method thread name: TestNG-test=testclass-3----test method name----testTwo
before method thread name: TestNG-test=testclass-1----test method name----testOne
before method thread name: TestNG-test=testclass-2----test method name----testThree

===============================================
TestSuite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

before method id: TestNG-test=testclass-2@1520676406
before method id: TestNG-test=testclass-2@1520676406
before method id: TestNG-test=testclass-2@1520676406
krmahadevan commented 5 years ago

@theric - Please provide a sample that can be used to reproduce this issue.

theric commented 5 years ago

add sample code

theric commented 5 years ago

@theric - Please provide a sample that can be used to reproduce this issue.

add sample code

theric commented 4 years ago

@krmahadevan add sample code