Unit Testing - Best Practice

I am using Visual Studio 2010 Professional with the MSTest platform to run unit tests. I have nasty production code for testing. The first problem is that the problematic code is in the constructor. I will show an example:

class ClassToTest
    {
        public SomeEnum UpperBorder;
        public SomeEnum LowerBorder;
        public int var1;
        private readonly SomeEnum2 _ethnicGroup;
        private readonly double _age;
        public int DataStart;
        public int DataEnd;
        public double[] DarkRedDarkYellow;
        public double[] DarkYellowGreen;
        public double[] GreenLightYellow;
        public double[] LightYellowLightRed;

        public ClassToTest(SomeEnum upperBorder, SomeEnum lowerBorder, int var1, SomeEnum2 ethnicGroup, int age)
        {
            UpperBorder = upperBorder;
            LowerBorder = lowerBorder;
            BscanIndex = bscanIndex;
            _ethnicGroup = ethnicGroup;
            _age = age;
            DataStart = 0;
            DataEnd = 0;
            DarkRedDarkYellow = null;
            DarkYellowGreen = null;
            GreenLightYellow = null;
            LightYellowLightRed = null;
        }
}

My question is:

  • write one test with a statement for each variable? or write a couple of tests and in each test check only one variable at a time? eg:

 

[TestMethod()]
public void ClassToTest_Constructor_upperBorder_PTest()
{
    //ACT
    var ob = new ClassToTest(SomeEnum.bor1, SomeEnum.bor2,10,SomeEnum2.Asian,10);
    //ASSERT
    Assert.IsNotNull(object);
    Assert.AreEqual(ob.upperBorder,SomeEnum.bor1);
}
  • Should I check if the constructor assigns the parameters to the private field correctly? Or if there is a property that returns this private field, but it performs another action, such as a trigger event, a log action, etc.

I can not find any information about this. Therefore, your advice will be most precious.

+5
4

. , , , . .

. A unit test .

, , . , , . , , , - , , , . , .

+6

: :

, , .

, .

, - . , .

. , , Unit Testing, .

.

+4

, , - :

  • , . ( ).
  • , :
    • , ( )
    • / , .

, . -, , ; , .

0

, ,

  • A separate project for unit testing.
  • One class for writing unit function tests in one class of core code.
  • Coverage Conditions Inside Functions
  • Test Driven development (TDD)

If you really want to know more (with examples), take a look at this tutorial.

C # unit tests - best practices   https://www.youtube.com/watch?v=grf4L3AKSrs

0
source

All Articles