Test Cases Not Working

Test Cases Not Working

de către Brad Strassburger-
Număr de răspunsuri: 3

I have a simple code where within a class a main method is calling a second method and printing out the result of some simple math. The code is fine. The system accepts the code and I have finally gotten it to accept the test cases, but it is not actually running the test case as expected. Instead of getting any answer, it is getting no answer.

This is the test case code

class Body{

    void myMethod(){

        int num1 = 8;

        int num2 = 5;

        int total = num1 + num2;

        System.out.println(total);

    }    

    public void main(String [] args){

        Body myObj = new Body();

        myObj.myMethod();

    }

}


Ca răspuns la Brad Strassburger

Re: Test Cases Not Working

de către Richard Lobb-
The first thing to do when a question isn't behaving as you expect is to turn on template debugging tro see exactly what code is being run. Copy the code that is being run out of CodeRunner into an IDE and run it there to make sure it works properly in that context.

If the code you're running works correctly in an IDE, post back here but please include:
  1. What question type you're using.
  2. What the answer is that you're grading. [You've given us the test code, but I'm puzzled as it seems self-contained.]
  3. A detailed explanation of exactly what happens when you click the Check button.
  4. An xml export of the question you're running (and also an export of its question prototype if it's not a standard built-in question type).

I suspect you're using the wrong question type. Have you read the Question type details for the question type, and inspected its template? [To see the template, temporarily click Customise and scroll down a bit.]

Ca răspuns la Richard Lobb

Re: Test Cases Not Working

de către Brad Strassburger-
Thank you for the feedback. I was able to get things working. The code answer always worked fine, I just seem to be having some difficulty getting my head around how the test cases are being executed.
I have attached a xml copy of the question to look at if anyone is interested. I know there is not much interest in java, but I am hoping that I can add a little to the repository if wanted.
Ca răspuns la Brad Strassburger

Re: Test Cases Not Working

de către Richard Lobb-
Thanks for sharing your question and for the offer to add to the repository. I now see what you're trying to achieve and why you are finding the testing problematic.

As a school teacher with small classes it can be convenient to ask questions with a somewhat vague specification (e.g. "write a class that does something interesting and has at least two methods, where one method calls the other"). You then manually check each student's code and offer one-on-one advice. 

With CodeRunner, however, you need to ask testable questions, where you can run code that definitively marks a students's code right or wrong. That means the specification of the question needs to be totally explicit about all the details. For example, rather than "a method that adds two numbers" you might have to say "a method with a signature int addNumbers(int n1, int n2) that returns the sum of its parameters n1 and n2". Or it might instead say that the function is of type void and prints the sum in some specified format.

Here are three questions that call for such a method:

1. Using the Java method question type:



2. Using the Java class question type:



3. Using the Java program question type:



The three approaches have different trade-offs. None is perfect. The last one is the easiest in many ways because no code is being hidden in the template, but has the disadvantage that the output when running from the keyboard includes echoing of the input as it is read, which doesn't happen when running from an input file, as with CodeRunner. [With Python we can simulate that in the python3_w_input question type, but I don't know of an easy way to do that in Java.]

Note that none of these methods lets you "see inside" the implementation that the student has chosen. So you can't easily ask students to, for example, use two methods where one method calls the other. [You could test the internal behaviour using Mocking - see https://www.unlogged.io/post/java-mocks-a-guide-to-mocking-in-java - but that's a couple of orders of magnitude more complicated.]

I attach a zip of the xml exports of the above three questions.