Probably walking down an a well-trodden path, but still: let me do this :)
The OOPClassBasicsTesterLibrary contains a ...library (!) to use wile writing unit tests to check for basic class making skills. For example, it allows you to check if the user created the correct properties and following the correct naming, returntype, and whether the get/set combo works as expected.
(Again, I'm pretty sure things like Roslyn will also do this)
- Add the OOPClassBacisTesterLibrary (nuget) to your unit test project: see https://www.nuget.org/packages/OOPClassBasicsTesterLibrary/
- Make sure an empty class with the correct name already exists in the project to test
- In your Unit test class make sure to create the analyzer and give it an instant of the class to test (static classes/methods cant be tested...yet )
TimsEpicClassAnalyzer tester = new TimsEpicClassAnalyzer(new StudentClassToMake());
- Start creating tests
-
CheckFullProperty
-
CheckAutoProperty
-
TestPropGetSet: test if the get and set work by checking of the expected value is return from the get after setting it.
-
TestBackingFieldProp: test if a backing field of a given name receives the correct information from a given property
-
CheckMethod: Check if the method is compliant to the basic requirements (name, returntype, parameters)
-
TestMethod: test if the method (optional: given a certain set of parameters) returns the expected result.
We use this in our basic object oriented programming classes C# (at the AP University Collega, informatics bachelor degree). In the first labs, student need to learn to write classes that adhere to some basic requirements (correct properties, naming, etc). This library allows the lecture to write unit tests the student can then run to check if his class meets the minimal requirements.
Imagine the student needs to write a class with the following requirements:
- It has a full property called
Diameter
(typeint
) with a backingfield calleddiameter
. (if the backing field has the same name as the full property, but starts with small char, no additional info needs to be passed to the method). - It has a full property called
Toppings
(typestring
) and it uses a backingfield calledtops
.
tester.CheckFullProperty("Diameter", typeof(int));
tester.CheckFullProperty("Toppings", typeof(string),"tops");
//Test if the backing field gets its value from the property (second parameter)
tester.TestBackingFieldProp("Toppings, "tops", "pineapple", "pineapple");
- It has an autoproperty called
IsPizza
of typebool
.
tester.CheckAutoProperty("IsPizza", typeof(bool));
- It has a full property called
Price
, with a backing fieldprice
. Type =double
. - Only positive value can be set. If a negative value is set, nothing happens and the property retains its current value.
string priceProp = "Price";
if(tester.CheckFullProperty(priceProp, typeof(double)))
{
double starttop = 66.68;
//test if we can save a positieve number. We expect the same number if we call the get.
tester.TestPropGetSet(priceProp, starttop, starttop);
//if we try to set a negative number, we expect the previous value (startop) to be returned after calling the get
tester.TestPropGetSet(priceProp, -1.0, starttop);
}
- The class has a method called
Compute
that should return anint
and expects two paramers of typeint
andstring
. - Running the method with parameters
3
andhello
should return the value5
. - The method should print the second parameter to the console.
if (tester.CheckMethod("Compute", typeof(int), new Type[] { typeof(int), typeof(string) }))
{
var arguments = new object[] { 3 , "hello" };
string consoleOutput = tester.TestMethod("Compute", arguments, 5);
Assert.AreEquals("hello",consoleOutput);
}