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 :)