Skip to content

Commit 59cac8c

Browse files
authored
Merge pull request #1742 from maths/iss1703-any
Allow test cases to ignore expected value of score or penalty.
2 parents e70fdd3 + b668743 commit 59cac8c

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

doc/en/STACK_question_admin/Testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The teacher can also examine by hand the outcomes generated by each step of each
4141
3. One example of each distinction you wish to make, i.e. if you have added specific feedback then provide an answer you expect to trigger this.
4242
4. Some "invalid" responses, especially if these are syntactically-valid expressions. E.g. If the answer is an equation such as \(y=2x+1\), then \(2x+1\) might be invalid if you have chosen the input option "check types". Adding a test case is useful to confirm this potential problem is caught by the question. Leave the fields empty and the expected answer note `NULL` to indicate this.
4343
9. If you leave the penalty field blank it will assume you mean the default penalty for the question.
44+
10. If you set either the score or penalty field to -1 then the test will pass on any value of that field, effectively ignoring the field for the purposes of this test case. This is not recommended (as you're not testing anymore!).
4445

4546
Normally test cases are constructed taking into account values of the question variables, so test-case construction can reflect any random variants. If you start the expected value of an input with the tag `RAW:` (case sensitive) then the remainder of your input will be used as a raw string. E.g. if your test case is `RAW:2 x` then your input test case will be `2 x`. Note, this feature does _not_ evaluate the expression further, and values of question variables will not be used as part of constructing the input for this test case. However, the potential response tree _will_ have access to the question variables later. For example, if `n:42` in the question variables then typically student's who input `n` will get a validation error. `RAW:n` allows you to create a test case, which will be displayed as `n` but later the PRTs will pick up the question variables. This feature is intended to test _invalid_ input rather than evaluation of test cases constructed from the random variables. We can't have it both ways!
4647

stack/questiontestresult.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ public function get_prt_states() {
199199
is_null($state->expectedscore) != is_null($state->score) ||
200200
abs($state->expectedscore - $state->score) > 10E-6
201201
) {
202-
$state->testoutcome = false;
203-
$reason[] = stack_string('score');
202+
// When the expected score is -1 the test does not fail.
203+
if ($state->expectedscore != -1) {
204+
$state->testoutcome = false;
205+
$reason[] = stack_string('score');
206+
}
204207
}
205208
// If the expected penalty is null then we use the question default penalty.
206209
$penalty = $state->expectedpenalty;
@@ -216,8 +219,11 @@ public function get_prt_states() {
216219
is_null($state->penalty) ||
217220
abs($penalty - $state->penalty) > 10E-6
218221
) {
219-
$state->testoutcome = false;
220-
$reason[] = stack_string('penalty');
222+
// When the expected penalty is -1 the test does not fail.
223+
if ($penalty != -1) {
224+
$state->testoutcome = false;
225+
$reason[] = stack_string('penalty');
226+
}
221227
}
222228
}
223229
[$noteresult, $messages] = $this->test_answer_note(

tests/questiontype_test.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ public function test_get_possible_responses_test4(): void {
143143
}
144144

145145
public function test_initialise_question_instance(): void {
146-
147146
$qdata = test_question_maker::get_question_data('stack', 'test3');
148147
$q = $this->qtype->make_question($qdata);
149148
$expectedq = test_question_maker::make_question('stack', 'test3');
@@ -193,17 +192,29 @@ public function test_question_tests_test3(): void {
193192
));
194193
$testcases[] = $qtest;
195194

196-
// This unit test runs a question test, with an input name as
197-
// the expected answer, which should work.
195+
// This unit test runs a question test, with an input name as the expected answer.
198196
$qtest = new stack_question_test('', ['ans2' => 'ans2']);
199197
$qtest->add_expected_result('even', new stack_potentialresponse_tree_state(
200198
1,
201-
true,
199+
false,
202200
1,
203201
0,
204202
'',
205203
['even-1-T']
206204
));
205+
$testcases[] = $qtest;
206+
207+
// This unit test runs a question test, accepting any score and penalty.
208+
$qtest = new stack_question_test('', ['ans2' => 'x^2']);
209+
$qtest->add_expected_result('even', new stack_potentialresponse_tree_state(
210+
1,
211+
true,
212+
-1,
213+
-1,
214+
'',
215+
['even-1-T']
216+
));
217+
$testcases[] = $qtest;
207218

208219
foreach ($testcases as $testcase) {
209220
$result = $testcase->test_question($questionid, $seed, context_system::instance());

0 commit comments

Comments
 (0)