You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, when an unittest.TestCase has two test methods with the same name, only the second one is run: the first one is silently ignored. In the past, the Python test suite had many bugs like that: see issue #60283. I just found a new bug today in test_sqlite3: issue #105557.
It would be nice to automatically detect when a test method is redefined. The problem is to avoid false alarms on method redefined on purpose: right now, I don't know if there are legit use cases like that.
It only emits a warning on the second Tests.test_bug() method definition. It doesn't emit a warning when RealTests redefines test_bug(): is it a legit use case to override an "abstract" test method of a "base" test case class? This case can also be detected if needed.
If someone wants to propose a PR based on my proof-of-concept, please go ahead. I'm not really interested to implement a fix for this issue right now. I just report the issue :-)
Also, it only works if tests with the same name are defined in the same class inherited from TestCase. It does not help if a test is overridden in a subclass or defined in a mixin class (or different mixin classes), which may be more common type of errors. But a solution which checks that the test does not override functions in superclasses would fail for many legitimate use cases.
I understand that unittest.TestCase class namespace cannot be customed, because unittest is written to be extended, and metaclasses and other hacks are common.
If it's not possible to change unittest.TestCase class, would it make sense to have a different solution to at least detect duplicate method names in the Python test suite? Maybe using a custom Python source code parser?
This Proof-Of-Concept doesn't understand decorators or how to build test case cases using mixin classes and class inheritance. But it may be able to catch some obvious bugs. If such tool detects false positives later, it can be enhanced later, no?
@sobolevn, is it possible to configure flake8 to only detect duplicate test method names? If so, we could simply add that to the CI, with no changes needed in the code base.
That's exactly how I found #105557 and why I created this issue... Maybe we can just use pyflakes in a CI job, only looking for this specific kind of issue?
Activity
vstinner commentedon Jun 9, 2023
If someone wants to propose a PR based on my proof-of-concept, please go ahead. I'm not really interested to implement a fix for this issue right now. I just report the issue :-)
cc @erlend-aasland @serhiy-storchaka
serhiy-storchaka commentedon Jun 9, 2023
Yes, this problem is known and was automatically detect was discussed before, but AFAIK it was not materialized in a PR.
The proposed solution would have false positive report for the following example:
Also, it only works if tests with the same name are defined in the same class inherited from TestCase. It does not help if a test is overridden in a subclass or defined in a mixin class (or different mixin classes), which may be more common type of errors. But a solution which checks that the test does not override functions in superclasses would fail for many legitimate use cases.
sobolevn commentedon Jun 10, 2023
This solution will also add a potential meta-class conflict for tools that already use metaclasses, like test plugins, custom loaders, etc.
erlend-aasland commentedon Jun 20, 2023
It seems like this is not an easy feature to add :( Suggesting to close this issue.
vstinner commentedon Jun 21, 2023
I understand that unittest.TestCase class namespace cannot be customed, because unittest is written to be extended, and metaclasses and other hacks are common.
If it's not possible to change unittest.TestCase class, would it make sense to have a different solution to at least detect duplicate method names in the Python test suite? Maybe using a custom Python source code parser?
Demo using
ast.NodeVisitor:This Proof-Of-Concept doesn't understand decorators or how to build test case cases using mixin classes and class inheritance. But it may be able to catch some obvious bugs. If such tool detects false positives later, it can be enhanced later, no?
erlend-aasland commentedon Jun 21, 2023
Perhaps it can be made as a tool that is run by patchcheck?
sobolevn commentedon Jun 21, 2023
pyflakescan do it:Or even better

flake8(it has more configuration options and usespyflakesinside):erlend-aasland commentedon Jun 21, 2023
@sobolevn, is it possible to configure flake8 to only detect duplicate test method names? If so, we could simply add that to the CI, with no changes needed in the code base.
sobolevn commentedon Jun 21, 2023
No, it will detect all duplicated functions and methods and some similar things: duplicated classes, imports, etc.
I think that all of these checks make sense in our case.
erlend-aasland commentedon Jun 27, 2023
Related:
(Oh, Victor already referenced this issue; sorry 'bout the noise 😄 )
vstinner commentedon Jun 27, 2023
That's exactly how I found #105557 and why I created this issue... Maybe we can just use pyflakes in a CI job, only looking for this specific kind of issue?
erlend-aasland commentedon Jun 27, 2023
+1
I created a follow-up issue in the core workflow repo. Let's close this and #60283 as superseded.
sobolevn commentedon Jun 27, 2023
I can take this on, I don't have any active issues at the moment to work on.
If anyone else wants it, I am totally fine :)
erlend-aasland commentedon Jun 27, 2023
Superseded by python/core-workflow#505