TestNG tests produce "Static configuration methods can cause unexpected behavior" warnings
See original GitHub issueExpected behavior
The test suite should not produce “Static configuration methods can cause unexpected behavior” warnings.
It looks like the tests should be adjusted to avoid unexpected behavior.
Actual behavior
There is a significant number of “Static configuration methods can cause unexpected behavior” warnings in the test suite.
Test case sample
TestNG > Regression2 > test.invokedmethodlistener.InvokedMethodListenerTest > issue87_method_orderning_with_disable_test_class STANDARD_OUT
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.C.someMethod3()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.C.someMethod3()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.A.someMethod1()]. Static configuration methods can cause unexpected behavior.
[Configuration] [WARN] Detected a static method [test.invokedmethodlistener.C.someMethod3()]. Static configuration methods can cause unexpected behavior.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
unable to execute test when data provider is mentioned
When i add "dataProvider" and "dataProviderClass" to my APpium test i am getting below error,
Read more >You dont need static driver or static methods
First, we create a BaseTest class as a parent for test classes. We move to it the driver object and make it static....
Read more >How To Solve- org.testng.TestNGException - YouTube
Many people are facing issue because of the TestNG version and its compatibility issues. Here is the error log looks likeorg. testng.
Read more >Problems in Parallel Execution With Static WebDriver
Now you can use this static WebDriver reference using its class name ... There are two simple TestNG classes with two @Test methods...
Read more >Testing Configuration Changes in Context to Prevent ...
Erroneous configuration values causing unexpected behavior ... Ctest: focus on testing production system configurations. • When configuration changes, test ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In fact, I suppose the
static
support was a feature at the beginning because JUnit did it. But it is not the TestNG approach and that’s why we added a warning. Current tests weren’t updated :pI like your idea about the flag for the tests using the
static
approach (when expected).@krmahadevan , well, the question was “does it still make sense to keep the warning?”. I could easily create a PR that would just remove the warning. However, if the warning was important, then we should not just remove it.
Ok, I did some research, and here’s the PR that added the warning: https://github.com/cbeust/testng/pull/2369#issuecomment-695747344
TL;DR is as follows:
@BeforeClass
(or even@BeforeSuite
) is static or not. Ultimately, there’s just a single instance, so it does not matter much if the configuration is static or not.With JUnit, the approach is different:
@BeforeAll
on an instance method would make no sense for JUnit since it would discard the instance anywayFrankly speaking, I am not sure which way is better (I believe both frameworks allow users to specify keep/recreate test instances). However, there might be cases when
BeforeSuite
does not really access instance methods (e.g. it just prints a message to a log or something). In that case, marking@BeforeSuite
method static in Java makes it super-clear for the reader that the method does not access/modify instance fields.I would suggest:
static
@BeforeSuite
and@AfterSuite
a non-issue (it aligns with https://github.com/cbeust/testng/issues/2294#issuecomment-614192473 )