<p>Consider the following:</p>
<ul>
<li>Create a C# unit test project, push this to github.</li>
<li>Create some interfaces that your students have to implement.</li>
<li>Create test scripts that test the implementations of these interfaces. (<a href="https://cseducators.stackexchange.com/a/7721/13465.">https://cseducators.stackexchange.com/a/7721/13465.</a>)</li>
<li>Create a branch for each student, give them only access to their own branch.</li>
<li>Use GitHub actions to automatically run the unity tests when a student pushes their code.</li>
</ul>
<p>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.</p>
<p>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.</p>
<hr />
<p>An alternative, that I think is better anyway.</p>
<ul>
<li>Create repo A containing the interfaces.</li>
<li>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.</li>
<li>Create repo C containing one example of an implemented interface.</li>
<li>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.)</li>
<li>Configure repo C so pull requests are verified by the code in repo A. (Not sure if this is possible, I assume it is.)</li>
</ul>

CC BY-SA 4.0
2023-05-31T08:39:50.990Z
2023-05-31T08:50:09.153Z
2023-05-31T08:50:09.153Z
Bas Visscher
<p>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.</p>
<p>Here's a breakdown of an nunit test:</p>
<pre class="lang-cs prettyprint-override"><code>[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);
   }
}
</code></pre>
<p><strong>Comments:</strong></p>
<ol>
<li>A class serves as a <em>container</em> for a series of tests.</li>
<li>The <code>TextFixture</code> attribute lets Nunit know about (1).  Test frameworks do automated discovery of tests.</li>
<li>The name of the unit test.</li>
<li>The <code>[Test]</code> attribute tells Nunit that we are declaring a test.</li>
<li>The ARRANGE section of a test creates the conditions for the test. This setup is trivial, but the arrange section can be quite complex!</li>
<li>The ACT section of a test actually runs the test or tests.</li>
<li>The ASSERT section makes sure that the appropriate result(s) took place.</li>
</ol>
<p>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.)</p>

CC BY-SA 4.0
2023-05-16T17:17:35.540Z
2023-05-16T17:17:35.540Z
Ben I.
<p>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.</p>
<p>EDIT:<br />
Looking to automate this testing as well through a C# app that I build or other means.</p>
<p>What tools exist to help perform this type of task?</p>

CC BY-SA 4.0
2023-05-16T22:52:11.243Z
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
CC BY-SA 4.0
2023-05-17T05:15:01.670Z
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
CC BY-SA 4.0
2023-06-07T05:50:32.660Z
@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
CC BY-SA 4.0
2023-06-07T05:51:49.603Z
@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
CC BY-SA 4.0
2023-06-07T05:53:47.727Z
@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
<unit-testing>
How can I unit test a class in a .cs file that is uploaded or otherwise provided to me?
# How can I unit test a class in a .cs file that is uploaded or otherwise provided to me?

(Asked by: username_0 on 2023-05-16)

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?

**Tags:** unit-testing

> **Comments:**
> **username_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._  
> **username_2**: 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.  
> **username_3**: @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?  
> **username_3**: @username_2: 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.  
> **username_2**: @username_3 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.  

---

## 2 answers

### Answer by username_5 on 2023-05-16. Score: 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:

A class serves as a container for a series of tests.
The TextFixture attribute lets Nunit know about (1).  Test frameworks do automated discovery of tests.
The name of the unit test.
The [Test] attribute tells Nunit that we are declaring a test.
The ARRANGE section of a test creates the conditions for the test. This setup is trivial, but the arrange section can be quite complex!
The ACT section of a test actually runs the test or tests.
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.)


---

### Answer by username_4 on 2023-05-31. Score: 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.)


---

