Group testing a simple collection class

Consider the following class:

public class MyIntSet
{
    private List<int> _list = new List<int>();

    public void Add(int num)
    {
        if (!_list.Contains(num))
            _list.Add(num);
    }

    public bool Contains(int num)
    {
        return _list.Contains(num);
    }
}

Following the principle of "only one test", suppose I want to test the "Add" function. Consider the following possibility for such a test:

[TestClass]
public class MyIntSetTests
{
    [TestMethod]
    public void Add_AddOneNumber_SetContainsAddedNumber()
    {
        MyIntSet set = new MyIntSet();
        int num = 0;

        set.Add(num);
        Assert.IsTrue(set.Contains(num));
    }
}

My problem with this solution is that it actually tests 2 methods : Add () and Contains (). Theoretically, in both cases there may be an error that appears only in scenarios where they are not called one after another. Of course, Contains () is now a server like a thin shell for List Contains (), which should not be tested on its own, but what if in the future it changes to something more complex? Perhaps a simple β€œthin pack” method should always be kept for testing purposes?

(, InternalsVisibleTo PrivateObject) _list- , , - - (, C5).

? - ?

, JC

+5
3

. , .

( ) : , , ; , (, - ..).

" , ", : -).

:

  • , , , .
    , , :-). , , .

  • (, InternalsVisibleTo) _list , [...]
    , . A unit test . unit test, " " -test; -).

+9

.

  • . , , Add. , , Contains()? , . , , , .Add() . , ? , .

  • , . , , . , .

, , , . , , .

+1

. - , add() contains() , . , add() add(), _list unit test.

[TestClass]
public void Add_AddOneNumber_SetContainsAddedNumber() {
    MyIntSet set = new MyIntSet();
    set.add(0);
    Assert.IsTrue(set._list.Contains(0));
}

This has two drawbacks. One: this requires access to the _list private variable, which is a bit complicated in C # (I recommend the reflection technique ). Two: it makes your test code dependent on the actual implementation of your Set implementation, which means you will have to modify the test if you ever change the implementation. I would never do this for something as simple as a collection class, but in some cases it can be useful.

+1
source

All Articles