Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

TestNG initializes data of other classes before running one test

51 views
Skip to first unread message

Александр Матюшенко

unread,
Nov 2, 2024, 2:13:37 AM11/2/24
to testng-dev
Hello everyone. Help me solve the problem.
I have many test classes in my project. By default, a config is written that runs all tests. Through the IntelliJ IDEA interface, I run any one test and before running the test, the data in all test classes is initialized. In my case, this is the creation of page objects, as well as any other objects. This greatly increases the time it takes to run one test through the development environment.

Maybe there is some setting for this?
ShooterScreenshot-226-01-11-24.png

⇜Krishnan Mahadevan⇝

unread,
Nov 4, 2024, 10:22:14 PM11/4/24
to testn...@googlegroups.com
Can you please share some test code that can be used to understand the question better?

You should be moving the instantiation of objects from constructor onto TestNG annotation driven methods for better control. But then again, this is just a generic recommendation.


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-dev+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/testng-dev/97b3b064-b49a-4440-9823-1c5ddddac6e0n%40googlegroups.com.

Александр Матюшенко

unread,
Nov 5, 2024, 6:22:06 AM11/5/24
to testng-dev
public class BottomSheetTests extends TestWrapper {

private PassportPage passportPage = new PassportPage();
private BottomSheet bottomSheet = new BottomSheet();
private GlobalSearchSidePanel globalSearchSidePanel = new GlobalSearchSidePanel();
private QrScanner qrScanner = new QrScanner();

private void precondition() {
browser.openPage(MAIN_PAGE, Users.UserDefault.CUS);
passportPage.loginWithoutSms(Users.UserDefault.LOGIN);
}

@Test(description = "Закрытие шторки свайпом", groups = {MOBILE})
public void closeBottomSheetBySwipe() {
precondition();
bottomSheet.swipeDown();
check.isUrlRight(ALL_PRODUCTS_PAGE);
}
}
When running another test from another class, the data of this class is immediately initialized. The more tests in a project, the slower the first test is launched. This is not the case in JUnit5, the test class data is initialized at the moment the test from this class is launched.
вторник, 5 ноября 2024 г. в 06:22:14 UTC+3, krishnan.ma...@gmail.com:

⇜Krishnan Mahadevan⇝

unread,
Nov 6, 2024, 11:56:25 PM11/6/24
to testn...@googlegroups.com
TestNG creates the test class instances at the beginning of execution.
TestNG will instantiate BottomSheetTests if its being inherited by other test classes. If this class is NOT in your hierarchy then it wont be executed.

TestNG and JUnit fundamentally have a difference in how the test class instances are created. TestNG shares the same instance between multiple test methods of the same test class. Junit I believe creates a new instance of the test class for every test method execution.

Would it be possible for you to share a sample project that can show us the problem so that it's easier to offer a proper solution?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Message has been deleted

Александр Матюшенко

unread,
Nov 8, 2024, 9:52:11 AM11/8/24
to testng-dev
I can't show the project because of the company's confidentiality.
I'll try to explain it differently.
For example, I have 2 classes BottomSheetTests and ChangeAvatarTests. They are not related to each other.
I run one test through IntelliJ IDEA in the ChangeAvatarTests class. Before running it, the data in the BottomSheetTests class is initialized, as well as in all other classes. I can initialize them in @BeforeClass, but this is extra code and you always need to make sure that other people who write tests do not forget to do this. This can be checked by setting breakpoints on the fields of the test classes.

четверг, 7 ноября 2024 г. в 07:56:25 UTC+3, krishnan.ma...@gmail.com:

Krishnan Mahadevan

unread,
Nov 9, 2024, 8:26:46 AM11/9/24
to testn...@googlegroups.com
What if you did something like below?


* Create a base class which all your tests extend from.
* Add the “@BeforeClass” initialisation within it.
* Have all your test classes extend from it.


You dont need to share your actual code. You could create a sample test project which mimics your actual scenario and which we can use to reproduce the problem that you are experiencing ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/
Reply all
Reply to author
Forward
0 new messages