test_object_accessed¶
-
test_object_accessed
(name, times=1, not_accessed_msg=None, state=None)¶ Test if object accessed
Checks whether an object, or the attribute of an object, are accessed
Parameters: - name (str) – the name of the object that should be accessed; can contain dots (for attributes)
- times (int) – how often the object specified in name should be accessed.
- not_accessed_msg (str) – custom feedback message when the object was not accessed.
Examples
Student code
import numpy as np
arr = np.array([1, 2, 3])
x = arr.shape
Solution code
import numpy as np
arr = np.array([1, 2, 3])
x = arr.shape
t = arr.dtype
SCT
test_object_accessed("arr")
: pass.test_object_accessed("arr.shape")
: pass.test_object_accessed("arr.dtype")
: fail.
def test_object_accessed(name,
times=1,
not_accessed_msg=None)
With test_object()
, you can check whether a student correctly created an object. However, in some cases, you might also be interested whether the student actually used this object to for example assign another object. test_object_accessed()
makes this possible; it is also possible to test object attributes.
The name
argument should be a string that specifies the name of the object, or the attribute of a certain object, for which you want to check if it was accesses. If the object resides inside a package, such as pi
in the math
package, use "math.pi"
. With times
, you can specify how often the object or attribute should have been accessed. With not_accessed_msg
you can override the automatically generated feedback message in case name
hasn’t been accessed often enough according to times
.
Example¶
To show how everything works, suppose you have the following submission of a student:
```
import numpy as np
import math as m
arr = np.array([1, 2, 3])
x = arr.shape
print(arr.data)
print(m.e)
```
Let’s have a look at some SCT function calls that either pass or fail and why.
test_object_accessed("arr")
- PASS: The objectarr
is accessed twice (inarr.shape
andarr.data
)test_object_accessed("arr", times=3)
- FAIL: The objetarr
is only accessed twice.test_object_accessed("arr.shape")
- PASS: Theshape
attribute ofarr
is accessed once.test_object_accessed("math.e")
- PASS: The objecte
inside themath
package is accessed once (the student uses the aliasm
, but that is not a problem. In case of an error, the automatically generated message will take this into account.)