Czy to jest test jednostkowy?

0

Piszę program dla sprawdzenia samego siebie. Chcę nauczyć się testować aplikacje, jednak posiadam tylko darmowe VisualStudio 2008. (express).

Z tego co widziałem nie ma wbudowanych modułów do testów jednostkowych, więc postanowiłem pisać je sam. Jednak nie wiem, czy idę w dobrą stronę.

Napisałem sobie klasę testującą:

class Test
    {
        public bool AreEqual(object o1, object o2)
        {
            if (o1.Equals(o2)) return true; else return false;
        }

        public bool AreNotEqual(object o1, object o2)
        {
            if (o1.Equals(o2) == false) return true;
            else return false;
        }

        public bool IsTrue(bool b)
        {
            if (b) return true;
            else return false;
        }

        public bool IsFalse(bool b)
        {
            if (!b) return true;
            else return false;
        }

        public bool IsInstanceOfType(object o1, object o2)
        {
            if (o1.GetType() == o2.GetType()) return true;
            else return false;
        }

        public bool IsNotInstanceOfType(object o1, object o2)
        {
            if (o1.GetType() != o2.GetType()) return true;
            else return false;
        }

        public bool IsNotNull(object o)
        {
            if (o != null) return true;
            else return false;
        }

        public bool IsNull(object o)
        {
            if (o == null) return true;
            else return false;
        }
    }

I teraz tworzę sobie osobną klasę testującą inną klasę.
Np mam strukturę RGB, która przechowuje oczywiście składowe piksela.

Klasa wygląda tak:

class RGBTest
    {
        private string Info;

        private void SaveTestResult(bool b, string name)
        {
            if (b) Info += "\n" + name + ": OK";
            else Info += "\n" + name + ": Błąd";
        }
        
        private void ConstructorTest()
        {
            RGB rgb = new RGB();
            this.SaveTestResult( ((rgb.A == 0) && (rgb.R == 0)
                && (rgb.G == 0)) && (rgb.B == 0), "RGB()");
        }

        private void ConstructorRGBTest()
        {
            RGB rgb = new RGB(0, 150, 255);
            this.SaveTestResult(((rgb.A == 255) && (rgb.R == 0)
                && (rgb.G == 150)) && (rgb.B == 255), "RGB(r, g, b)");
        }

        private void ConstructorARGBTest()
        {
            RGB rgb = new RGB(100, 0, 150, 255);
            this.SaveTestResult(((rgb.A == 100) && (rgb.R == 0)
                && (rgb.G == 150)) && (rgb.B == 255), "RGB(a, r, g, b)");
        }

        private void ConstructorCopyTest()
        {
            RGB myrgb = new RGB(100, 0, 150, 255);
            RGB rgb = new RGB(myrgb);
            this.SaveTestResult(((rgb.A == 100) && (rgb.R == 0)
                && (rgb.G == 150)) && (rgb.B == 255), "RGB(rgb)");
        }
        

        public string TEST()
        {
            this.ConstructorTest();
            this.ConstructorRGBTest();
            this.ConstructorARGBTest();
            this.ConstructorCopyTest();
            return this.Info;
        }

    }

Teraz już tylko wywołuje sobie funkcję "Test" i w zmiennej "Info" mam wyniki testów.
Jest to wygodne i miłe. Całkiem przyjemnie mi się przy tym pracuje, a gdy modyfikuję coś wrażliwego, to od razu wiem, czy to coś działa czy nie itd.
Jednak... czy to można nazwać testem jednostkowym, a jeśli nie to jakim testem to jest?
Czy tego typu test jest ok, czy może należy coś w nim poprawić?

Z góry dziękuję za odpowiedź i pomoc, gdyż jestem początkujący w testowaniu programu :)

0
 class Test
    {
        public bool AreEqual(object o1, object o2)
        {
            return o1.Equals(o2);
        }

        public bool AreNotEqual(object o1, object o2)
        {
            return o1.Equals(o2);
        }

        public bool IsTrue(bool b)
        {
            return b;
        }

        public bool IsFalse(bool b)
        {
            return !b;
        }

        public bool IsInstanceOfType(object o1, object o2)
        {
            return o1.GetType() == o2.GetType();
        }

        public bool IsNotInstanceOfType(object o1, object o2)
        {
            return !(o1.GetType() != o2.GetType());
        }

        public bool IsNotNull(object o)
        {
            return o != null;
        }

        public bool IsNull(object o)
        {
            return o == null;
        }
    }

Teoretycznie można by to nazwać testem jednostkowym, lecz zamiast zapisywać do pliku polecam np. NUnit, który to zautomatyzuje i będziesz mógł sobie do CV dopisać znajomość frameworka ;)

0
emfałsi napisał(a):
 class Test
    {
        public bool AreEqual(object o1, object o2)
        {
            return o1.Equals(o2);
        }

        public bool AreNotEqual(object o1, object o2)
        {
            return o1.Equals(o2);
        }

        public bool IsTrue(bool b)
        {
            return b;
        }

        public bool IsFalse(bool b)
        {
            return !b;
        }

        public bool IsInstanceOfType(object o1, object o2)
        {
            return o1.GetType() == o2.GetType();
        }

        public bool IsNotInstanceOfType(object o1, object o2)
        {
            return !(o1.GetType() != o2.GetType());
        }

        public bool IsNotNull(object o)
        {
            return o != null;
        }

        public bool IsNull(object o)
        {
            return o == null;
        }
    }

Teoretycznie można by to nazwać testem jednostkowym, lecz zamiast zapisywać do pliku polecam np. NUnit, który to zautomatyzuje i będziesz mógł sobie do CV dopisać znajomość frameworka ;)

public bool AreNotEqual(object o1, object o2)
        {
            return !o1.Equals(o2);
        }
0

Twoja wersja:

        public bool AreEqual(object o1, object o2)
        {
            if (o1.Equals(o2)) return true; else return false;
        }

Krócej:

        public bool AreEqual(object o1, object o2)
        {
            return o1.Equals(o2));
        }

Twoja wersja:

        public bool AreNotEqual(object o1, object o2)
        {
            if (o1.Equals(o2) == false) return true;
            else return false;
        }

Krócej:

        public bool AreNotEqual(object o1, object o2)
        {
            return !o1.Equals(o2);
        }

Twoja wersja (mistrzostwo):

        public bool IsTrue(bool b)
        {
            if (b) return true;
            else return false;
        }

Hmm...

        public bool IsTrue(bool b)
        {
            return b;
        }

Twoja wersja:

        public bool IsFalse(bool b)
        {
            if (!b) return true;
            else return false;
        }

Czyli:

        public bool IsFalse(bool b)
        {
            return !b;
        }

Kolejne cztery:

        public bool IsInstanceOfType(object o1, object o2)
        {
            if (o1.GetType() == o2.GetType())
                return true;
            else
                return false;
        }

        public bool IsNotInstanceOfType(object o1, object o2)
        {
            if (o1.GetType() != o2.GetType())
                return true;
            else
                return false;
        }

        public bool IsNotNull(object o)
        {
            if (o != null)
                return true;
            else
                return false;
        }

        public bool IsNull(object o)
        {
            if (o == null)
                return true;
            else
                return false;
        }

Są równoważne:

        public bool IsInstanceOfType(object o1, object o2)
        {
            return o1.GetType() == o2.GetType();
        }

        public bool IsNotInstanceOfType(object o1, object o2)
        {
            return o1.GetType() != o2.GetType();
        }

        public bool IsNotNull(object o)
        {
            return o != null;
        }

        public bool IsNull(object o)
        {
            return o == null;
        }

Kiepsko z tymi ifami :>

Do tematu, bo już widzę że wszyscy się podobnie jak ja do tego przyczepili - ogólnie jakieś symptomy bycia testem jednostkowym to ma...
Ale bardzo dużo tutaj męczenia się z niepotrzebnymi szczegółami - zamiast tworzyć własną obsługę wszystkiego od zera, możesz użyć jakiegoś frameworka typu NUnit, xUnit, etc.

Twój kod przepisany na NUnit:

    [TestFixture]
    class RgbTest
    {
        [Test]
        private void ConstructorTest()
        {
            RGB rgb = new RGB();
            Assert.True((rgb.A == 0) && (rgb.R == 0)
                && (rgb.G == 0) && (rgb.B == 0));
        }

        [Test]
        private void ConstructorRGBTest()
        {
            RGB rgb = new RGB(0, 150, 255);
            Assert.True((rgb.A == 255) && (rgb.R == 0)
                && (rgb.G == 150) && (rgb.B == 255));
        }

        [Test]
        private void ConstructorARGBTest()
        {
            RGB rgb = new RGB(100, 0, 150, 255);
            Assert.True(((rgb.A == 100) && (rgb.R == 0)
                && (rgb.G == 150) && (rgb.B == 255)));
        }

        [Test]
        private void ConstructorCopyTest()
        {
            RGB myrgb = new RGB(100, 0, 150, 255);
            RGB rgb = new RGB(myrgb);
            Assert.True(((rgb.A == 100) && (rgb.R == 0)
                && (rgb.G == 150) && (rgb.B == 255)));
        }
    }
0

Najmocniej dziękuję.
To mi wiele wyjaśnia.

A facepalma jakiego złapałem po tych moich ifach sobie nie wyobrażacie. (pisałem to wieczorem i takie są efekty :D).
Jeszcze raz dziękuję za pomoc, rady i poprawki.

1

Można, ale skoro już są gotowe rozwiązania (choćby Boost.Test) to po co?

http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/

1 użytkowników online, w tym zalogowanych: 0, gości: 1