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
6 changes: 6 additions & 0 deletions grade/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def decorate(func):
- `visible` (default): test case will always be shown
"""

name = partial(static, "__name__")
name.__doc__ = """ Simple decorator to add a __name__ property to a function

Usage: @name("Functionality Test")
"""


def leaderboard(name=None, order="desc"):
""" Decorator that indicates that a test corresponds to a leaderboard column
Expand Down
13 changes: 13 additions & 0 deletions grade/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ def visibility(self, visibility: str) -> None:
""" Sets the visibility of the test. """
self.setattr("__visibility__", visibility)

@property
def name(self) -> str:
""" Returns the name of the test.

This controls which name the students will see for a test.
"""
return getattr(self.getTest(), "__name__", None)

@name.setter
def name(self, name: str):
""" Sets the name of the test. """
self.setattr("__name__", name)

@property
def leaderboard(self) -> dict:
""" Returns a dictionary with all leaderboard attributes. """
Expand Down
14 changes: 9 additions & 5 deletions grade/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ def getExceptions(self, test) -> List[Tuple]:
return [m for f, m in [*self.failures, *self.errors] if f == test]

def getName(self, test):
name = self.getattr(test, "__qualname__")
# TODO: Walrus once python 3.8 is supported.
description = test.shortDescription()
if description:
name = f"{name}: {description}"
# If a user-defined name exists, use it.
name = self.getattr(test, "__name__", None)
if not name:
name = self.getattr(test, "__qualname__")
# TODO: Walrus once python 3.8 is supported.
description = test.shortDescription()
if description:
name = f"{name}: {description}"

return name

def getScore(self, test):
Expand Down
8 changes: 8 additions & 0 deletions test/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def test_visible():
self.assertEqual(test_visible.__visibility__, "visible")
return

def test_name(self):
@name("Test Name")
def test_name_exists():
return

self.assertEqual(test_name_exists.__name__, "Test Name")
return

def test_leaderboard(self):
@leaderboard()
def test_defaults():
Expand Down
12 changes: 12 additions & 0 deletions test/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ def test_something(self):
self.assertEqual(x.test_something.__visibility__, "invisible")
return

def test_name(self):

class Test(ScoringMixin):
def test_name_exists(self):
self.name = "Name Test"
assert self.name == "Name Test"

x = Test()
x.test_name_exists()
self.assertEqual(x.test_name_exists.__name__, "Name Test")
return

def test_leaderboard(self):
""" Can we modify and recall a tests leaderboard standing? """

Expand Down