Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def pytest_collection_modifyitems(self, session, config, items):
def _sort_by_lineno(item):
test_id = Hierarchy(item.nodeid)
source = Path(item.fspath)
return TestOrder.lineno(test_id, source)
return TestOrder.get_testInfo(test_id, source).lineno

items.sort(key=_sort_by_lineno)

Expand Down
21 changes: 12 additions & 9 deletions runner/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,21 @@ def get_hierarchy(self, name: Hierarchy) -> Hierarchy:


@classmethod
def lineno(cls, test_id: Hierarchy, source: Path) -> int:
def get_testInfo(cls, test_id: Hierarchy, source: Path) -> TestInfo:
"""
Returns the line that the given test was defined on.
Returns the test's TestInfo (lineno, end_lineno, variants).
"""
if test_id not in cls._cache:
tree = parse(source.read_text(), source.name)
cls(Hierarchy(test_id.split("::")[0])).visit(tree)
return cls._cache[test_id].lineno
cls(Hierarchy(test_id.split("::")[0])
).visit(tree)

if test_id not in cls._cache:
#possibly a parametrized test. get the parametrized function's lineno instead
#in that case, test_id would be like path/file.py::parametrized_function[param_id]
test_id = Hierarchy(test_id.split('[')[0])

return cls._cache[test_id]


@classmethod
Expand All @@ -103,14 +110,10 @@ def function_source(cls, test_id: Hierarchy, source: Path) -> str:
:return: str of the source code of the given test.
"""
text = source.read_text()
testinfo = cls._cache[test_id]
testinfo = cls.get_testInfo(test_id, source)

lines = text.splitlines()[testinfo.lineno: testinfo.end_lineno + 1]

if test_id not in cls._cache:
tree = parse(text, source.name)
cls(Hierarchy(test_id.split("::")[0])).visit(tree)

if not lines[-1]:
lines.pop()

Expand Down
2 changes: 2 additions & 0 deletions test/example-parametrized/example_parametrized.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def plus1(x):
return x + 1
18 changes: 18 additions & 0 deletions test/example-parametrized/example_parametrized_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from example_parametrized import plus1

params = [
pytest.param(1, 2, id="test1"),
pytest.param(2, 3, id="test2"),
pytest.param(3, 4, id="test3"),
]

@pytest.mark.parametrize(("x", "y"), params)
def test_plus1_parameter(x, y):
assert plus1(x) == y

def test_unparametrized(x=1,y=2):
assert x+1

if __name__ == '__main__':
pytest.main()
47 changes: 47 additions & 0 deletions test/example-parametrized/results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"version": 3,
"status": "pass",
"tests": [
{
"name": "test plus1 parameter[test1]",
"status": "pass",
"test_code": "assert plus1(x) == y",
"task_id": 0,
"filename": "test/example-parametrized/example_parametrized_test.py",
"line_no": 9,
"duration": 9.589799446985126e-05,
"score": 2.5
},
{
"name": "test plus1 parameter[test2]",
"status": "pass",
"test_code": "assert plus1(x) == y",
"task_id": 0,
"filename": "test/example-parametrized/example_parametrized_test.py",
"line_no": 9,
"duration": 8.842398528940976e-05,
"score": 2.5
},
{
"name": "test plus1 parameter[test3]",
"status": "pass",
"test_code": "assert plus1(x) == y",
"task_id": 0,
"filename": "test/example-parametrized/example_parametrized_test.py",
"line_no": 9,
"duration": 7.641100091859698e-05,
"score": 2.5
},
{
"name": "test unparametrized",
"status": "pass",
"test_code": "assert x+1",
"task_id": 0,
"filename": "test/example-parametrized/example_parametrized_test.py",
"line_no": 13,
"duration": 7.80870032031089e-05,
"score": 2.5
}
],
"max_score": 10
}