0

Here is my situation. I teach a 'Programming with classes' course. The students are given a design of class to implement in c#. They will then create a .cs file for the class. I would like to give them the option of uploading (Or some other method) that file to a program that will then test their class and methods for certain inputs and outputs. The class and methods would be expected to follow the design EXACTLY.

EDIT:
Looking to automate this testing as well through a C# app that I build or other means.

What tools exist to help perform this type of task?

B-Rad
  • 101
  • 1
    Re, "...expected to follow the design exactly." I've heard various opinions throughout my career about what is, and what is not, "design." But, I've never heard of a design that could be tested. AFAIK, the thing that can be tested is an _implementation,_ and the purpose of the test is to prove whether or not the implementation satisfies specific _requirements._ – Solomon Slow May 16 '23 at 22:52
  • Is there a reason not to simply give them the test file as part of a C# project? If you're concerned they'll "know too much", could you give them a simpler set of sample tests, then ask them to write their own tests to cover edge cases? You can run the submissions on the full test suite for grading with a short script pretty simply, or with a fancier run-on-submit-with-live feedback platform like Gradescope. – ggorlen May 17 '23 at 05:15
  • @SolomonSlow: Semantically, interfaces are designed with a particular behavior in mind. Not the implementation, but what a consumer of the interface will generally expect. This matches OP's question of testing this component's implementation, i.e. does it fulfill the (designed) contract that it was expected to implement? – Flater Jun 07 '23 at 05:50
  • @ggorlen: Giving students the test cases will open the door to them overfitting their solution to the specific values being tested with. While it's not bad for them to get comfortable with testing somewhere during the curriculum, it is unclear whether that is right now. For the question being posted, the tests are being used as a teacher grading tool rather than a student development tool, and that changes things as to what information you expose to students. – Flater Jun 07 '23 at 05:51
  • @Flater It may not be, but as I suggested, you would give them a small sample harness, not the whole thing. If they don't add their own tests and cover edge cases, they'll be caught by the submission tests. The point is mainly to communicate the basic idea so they aren't left totally adrift having to guess how they're code is used by the submission suite. This is pretty much standard practice. – ggorlen Jun 07 '23 at 05:53

2 Answers2

1

You're essentially looking for unit testing. In C#, I am partial to nunit, though there are other testing suites, and they all do roughly the same thing.

Here's a breakdown of an nunit test:

[TestFixture] // 2
public class CalculatorTests // 1
{
   [Test] // 4
   public void Sum_of_two_numbers() // 3
   {
      // Arrange
      double first = 10;
      double second = 20;
      var calculator = new Calculator();

      // Act
      double result = calculator.Sum(first, second);

      // Assert
      Assert.Equal(30, result);
   }
}

Comments:

  1. A class serves as a container for a series of tests.
  2. The TextFixture attribute lets Nunit know about (1). Test frameworks do automated discovery of tests.
  3. The name of the unit test.
  4. The [Test] attribute tells Nunit that we are declaring a test.
  5. The ARRANGE section of a test creates the conditions for the test. This setup is trivial, but the arrange section can be quite complex!
  6. The ACT section of a test actually runs the test or tests.
  7. The ASSERT section makes sure that the appropriate result(s) took place.

If you want a web tool that simplifies your workflow tremendously, you can use something like GitHub Classroom or CodingRooms, which will both accept submissions and apply the tests automatically (and give you a nice readout as well!), though be aware that these are both paid platforms. (I was especially impressed by CodingRooms at a workshop I attended.)

Ben I.
  • 32,726
  • 11
  • 68
  • 151
0

Consider the following:

  • Create a C# unit test project, push this to github.
  • Create some interfaces that your students have to implement.
  • Create test scripts that test the implementations of these interfaces. (https://cseducators.stackexchange.com/a/7721/13465.)
  • Create a branch for each student, give them only access to their own branch.
  • Use GitHub actions to automatically run the unity tests when a student pushes their code.

I'm not quite sure about the possibilities in GitHub when it comes to rights and visibility settings. You wouldn't want students to see their classmates code of course.

One way around this would be to let the students fork your repo and make pull requests. I think its possible to let Github check the pull requests using your test cases.


An alternative, that I think is better anyway.

  • Create repo A containing the interfaces.
  • Create repo B containing the test project. Using repo A as a submodule. Repo B should be private, so the students can't see your test code.
  • Create repo C containing one example of an implemented interface.
  • Let the students fork repo C and let them create pull requests when they are finished. (Git will track when students did their work, so no more excuses for missing deadlines.)
  • Configure repo C so pull requests are verified by the code in repo A. (Not sure if this is possible, I assume it is.)