Skip to content

Commit b6cc438

Browse files
authored
Merge pull request github#9368 from RasmusWL/test-model-api-graphs
Python: Port test model to API graphs
2 parents 7ca0144 + 420dea0 commit b6cc438

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import python
2+
private import semmle.python.ApiGraphs
23

34
abstract class TestScope extends Scope { }
45

5-
// don't extend Class directly to avoid ambiguous method warnings
6-
class UnitTestClass extends TestScope {
6+
class UnitTestClass extends TestScope, Class {
77
UnitTestClass() {
8-
exists(ClassValue cls | this = cls.getScope() |
9-
cls.getABaseType+() = Module::named("unittest").attr(_)
10-
or
11-
cls.getABaseType+().getName().toLowerCase() = "testcase"
8+
exists(API::Node testCaseClass, string testCaseString |
9+
testCaseString.matches("%TestCase") and
10+
testCaseClass = any(API::Node mod).getMember(testCaseString)
11+
|
12+
this.getParent() = testCaseClass.getASubclass*().getAnImmediateUse().asExpr()
1213
)
1314
}
1415
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
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 |
41
| test_foo.py:3:1:3:15 | Function test_foo |
52
| unittest_test.py:3:1:3:33 | Class FooTest |
63
| unittest_test.py:4:5:4:25 | Function test_valid |
4+
| unittest_test.py:7:1:7:49 | Class FunctionFooTest |
5+
| unittest_test.py:8:5:8:25 | Function test_valid |
6+
| unittest_test.py:11:1:11:50 | Class AsyncTest |
7+
| unittest_test.py:12:11:12:31 | Function test_valid |
8+
| unittest_test.py:17:1:17:45 | Class MyDjangoUnitTest |
9+
| unittest_test.py:18:5:18:25 | Function test_valid |
10+
| unittest_test.py:23:1:23:56 | Class MyFlaskUnitTest |
11+
| unittest_test.py:24:5:24:25 | Function test_valid |
12+
| unittest_test.py:29:1:29:59 | Class MyTornadoUnitTest |
13+
| unittest_test.py:30:5:30:25 | Function test_valid |

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,29 @@
33
class FooTest(unittest.TestCase):
44
def test_valid(self):
55
pass
6+
7+
class FunctionFooTest(unittest.FunctionTestCase):
8+
def test_valid(self):
9+
pass
10+
11+
class AsyncTest(unittest.IsolatedAsyncioTestCase):
12+
async def test_valid(self):
13+
pass
14+
15+
# django -- see https://docs.djangoproject.com/en/4.0/topics/testing/overview/
16+
import django.test
17+
class MyDjangoUnitTest(django.test.TestCase):
18+
def test_valid(self):
19+
pass
20+
21+
# flask -- see https://pythonhosted.org/Flask-Testing/
22+
import flask_testing
23+
class MyFlaskUnitTest(flask_testing.LiveServerTestCase):
24+
def test_valid(self):
25+
pass
26+
27+
# tornado -- see https://www.tornadoweb.org/en/stable/testing.html#tornado.testing.AsyncHTTPTestCase
28+
import tornado.testing
29+
class MyTornadoUnitTest(tornado.testing.AsyncHTTPTestCase):
30+
def test_valid(self):
31+
pass

python/ql/test/query-tests/Statements/asserts/assert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def ok_assert_false(x):
6161
if x:
6262
assert 0==1, "Ok"
6363

64-
class TestCase:
65-
pass
64+
from unittest import TestCase
65+
6666

6767
class MyTest(TestCase):
6868
def test_ok_assert_in_test(self, x):

0 commit comments

Comments
 (0)