Skip to content

Commit de08433

Browse files
authored
Merge pull request github#3212 from RasmusWL/python-fix-tests-filter
Python: Fix (some) shortcomings of tests filter
2 parents cbe417f + 46ecbef commit de08433

File tree

7 files changed

+29
-12
lines changed

7 files changed

+29
-12
lines changed

python/ql/src/Statements/AssertLiteralConstant.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import semmle.python.filters.Tests
1717
from Assert a, string value
1818
where
1919
/* Exclude asserts inside test cases */
20-
not a.getScope() instanceof Test and
20+
not a.getScope().getScope*() instanceof TestScope and
2121
exists(Expr test | test = a.getTest() |
2222
value = test.(IntegerLiteral).getN()
2323
or

python/ql/src/semmle/python/filters/Tests.qll

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ abstract class TestScope extends Scope { }
55
// don't extend Class directly to avoid ambiguous method warnings
66
class UnitTestClass extends TestScope {
77
UnitTestClass() {
8-
exists(ClassObject c | this = c.getPyClass() |
9-
c.getASuperType() = theUnitTestPackage().attr(_)
8+
exists(ClassValue cls | this = cls.getScope() |
9+
cls.getABaseType+() = Module::named("unittest").attr(_)
1010
or
11-
c.getASuperType().getName().toLowerCase() = "testcase"
11+
cls.getABaseType+().getName().toLowerCase() = "testcase"
1212
)
1313
}
1414
}
1515

16-
PackageObject theUnitTestPackage() { result.getName() = "unittest" }
17-
1816
abstract class Test extends TestScope { }
1917

18+
/** Class of test function that uses the `unittest` framework */
2019
class UnitTestFunction extends Test {
2120
UnitTestFunction() {
2221
this.getScope+() instanceof UnitTestClass and
@@ -37,3 +36,11 @@ class NoseTestFunction extends Test {
3736
this.(Function).getName().matches("test%")
3837
}
3938
}
39+
40+
/** Class of functions that are clearly tests, but don't belong to a specific framework */
41+
class UnknownTestFunction extends Test {
42+
UnknownTestFunction() {
43+
this.(Function).getName().matches("test%") and
44+
this.getEnclosingModule().getFile().getShortName().matches("test_%.py")
45+
}
46+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
| Class MyTest |
2-
| Function test_1 |
3-
| Function test_2 |
1+
| test.py:4:1:4:23 | Class MyTest |
2+
| test.py:6:5:6:21 | Function test_1 |
3+
| test.py:9:5:9:21 | Function test_2 |
4+
| test_foo.py:3:1:3:15 | Function test_foo |
5+
| unittest_test.py:3:1:3:33 | Class FooTest |
6+
| unittest_test.py:4:5:4:25 | Function test_valid |

python/ql/test/library-tests/filters/tests/Filter.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import python
22
import semmle.python.filters.Tests
33

44
from TestScope t
5-
select t.toString()
5+
select t

python/ql/test/library-tests/filters/tests/test.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
class TestCase:
42
pass
53

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This is running under some unknown framework, but is clearly a test!
2+
3+
def test_foo():
4+
assert True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import unittest
2+
3+
class FooTest(unittest.TestCase):
4+
def test_valid(self):
5+
pass

0 commit comments

Comments
 (0)