Visual Studio Unit Testing Framework .NET, Visual Studio 2005 . , , [TestClass]. , . , , Test.
, , . , , Test.
. [TestInitialize] -. [TestCleanup] , , , , .
, . . , . [ClassInitialize], [ClassCleanup]. .
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test
{
[TestClass]
public class TestClass
{
public TestClass(){}
//
private TestContext testContextInstance;
public TestContext getTestContext(){
return testContextInstance;
}
public void setTestContext(TestContext testContext){
this.testContextInstance = testContextInstance;
}
[ClassInitialize]
public static void TestClassInitialize(TestContext testContext) {
/* */
}
[ClassCleanup]
public static void TestCClassCleanup(){
/* */
}
[TestInitialize]
public void TestInitialize(){
/* */
}
[TestCleanup]
public void TestCleanup(){
/* */
}
[TestMethod]
public void TestMethod(){
/* */
}
}
}
, , Assert.
Assert 6.1.
6.1 Assert
Assert.AreEqual | , . , . |
Assert.AreNotEqual | , . , . |
Assert.AreSame | , . , . |
Assert.AreNotSame | , . , . |
Assert.Fail | - . |
Assert.IsTrue | , . . |
Assert.IsFalse | , . . |
Assert.IsNull | , null. , null. |
Assert.IsNotNull | , null. , null. |
Assert.IsInstanceOfType | , . , . |
Assert.IsNotInstanceOfType | , . , . |
|
|
DAO, .
Unit Testing Framework, Visual Studio 2008.
1. Windows ( , , , Windows, , , lab6, , ).
2. dao, domain, mapping. , . , , Total Commander.
dao dao . 6.1.
5.1 dao dao WEB-
domain mapping .
, ( dao , , dao ). domain mapping. .
3. . :
Npgsql.dll |
Mono.Security.dll |
NHibernate.dll |
Antlr3.Runtime.dll |
Iesi.Collections.dll |
log4net.dll |
Castle.DynamicProxy2.dll |
Castle.Core.dll |
NHibernate.ByteCode.Castle.dll |
FluentNHibernate.dll |
namespace lab4 lab6 :
|
|
namespace lab4.dao { abstract public class DAOFactory { public abstract IStudentDAO getStudentDAO(); public abstract IGroupDAO getGroupDAO(); } } | namespace lab6.dao { abstract public class DAOFactory { public abstract IStudentDAO getStudentDAO(); public abstract IGroupDAO getGroupDAO(); } } |
, . , , DAO.
4. ( , , , , , lab6, lab6Test, ). . , : , , , .
5. - UnitTest1, TestMethod1. ( , , ), , . , .
6. lab6 lab6.
7. , . GroupDAO StudentDAO, GenericDAO, GenericDAO . : TestGenericDAO, TestGroupDAO, TestStudentDAO. TestGenericDAO GenericDAO, TestGroupDAO GroupDAO, TestStudentDAO StudentDAO. - [TestMethod]. , - [TestInitialize], , - [TestCleanup]. , .
8. TestGenericDAO :
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Criterion;
using System.Collections.Generic;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;
using NHibernate.Tool.hbm2ddl;
using System.Reflection;
using FluentNHibernate.Mapping;
using NHibernate.Cfg;
using lab6.mapping;
using lab6.dao;
using lab6.domain;
namespace lab6
{
[TestClass()]
public abstract class TestGenericDAO<T> where T:EntityBase
{
protected static ISessionFactory factory;
protected static ISession session;
protected DAOFactory daoFactory;
protected TestContext testContextInstance;
/** DAO that will be tested */
protected IGenericDAO<T> dao = null;
/** First entity that will be used in tests */
protected T entity1 = null;
/** Second entity that will be used in tests */
protected T entity2 = null;
|
|
/** Third entity that will be used in tests */
protected T entity3 = null;
public TestGenericDAO()
{
session = openSession("localhost", 5432, "university",
"postgres", "111111");
}
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
/*Getting dao this test case works with*/
public IGenericDAO<T> getDAO()
{
return dao;
}
/*Setting dao this test case will work with*/
public void setDAO(IGenericDAO<T> dao)
{
this.dao = dao;
}
[ClassCleanup]
public static void ClassCleanup()
{
session.Close();
}
[TestInitialize]
public void TestInitialize()
{
Assert.IsNotNull(dao,
"Please, provide IGenericDAO implementation in constructor");
createEntities();
Assert.IsNotNull(entity1, "Please, create object for entity1");
Assert.IsNotNull(entity2, "Please, create object for entity2");
Assert.IsNotNull(entity3, "Please, create object for entity3");
checkAllPropertiesDiffer(entity1, entity2);
checkAllPropertiesDiffer(entity1, entity3);
checkAllPropertiesDiffer(entity2, entity3);
saveEntitiesGeneric();
}
[TestCleanup]
public void TestCleanup()
{
try {
if ((entity1 = dao.GetById(entity1.Id))!= null)
dao.Delete(entity1);
} catch (Exception){
Assert.Fail("Problem in cleanup method");
}
try {
if ((entity2 = dao.GetById(entity2.Id))!= null)
dao.Delete(entity2);
} catch (Exception) {
Assert.Fail("Problem in cleanup method");
}
try {
if ((entity3 = dao.GetById(entity3.Id))!= null)
dao.Delete(entity3);
} catch (Exception) {
Assert.Fail("Problem in cleanup method");
}
entity1 = null;
entity2 = null;
entity3 = null;
}
[TestMethod]
public void TestGetByIdGeneric()
{
T foundObject = null;
// Should not find with inexistent id
try
{
long id = DateTime.Now.ToFileTime();
foundObject = dao.GetById(id);
Assert.IsNull(foundObject, "Should return null if id is inexistent");
}
catch (Exception)
{
Assert.Fail("Should return null if object not found");
}
// Getting all three entities
getEntityGeneric(entity1.Id, entity1);
getEntityGeneric(entity2.Id, entity2);
getEntityGeneric(entity3.Id, entity3);
}
[TestMethod]
public void TestGetAllGeneric()
{
List<T> list = getListOfAllEntities();
Assert.IsTrue(list.Contains(entity1),
"After dao method GetAll list should contain entity1");
Assert.IsTrue(list.Contains(entity2),
"After dao method GetAll list should contain entity2");
Assert.IsTrue(list.Contains(entity3),
"After dao method GetAll list should contain entity3");
}
[TestMethod]
public void TestDeleteGeneric()
{
try
{
dao.Delete((T)null);
Assert.Fail("Should not delete entity will null id");
}
catch (Exception)
{
}
// Deleting second entity
try
{
dao.Delete(entity2);
}
catch (Exception)
{
Assert.Fail("Deletion should be successful of entity2");
}
// Checking if other two entities can be still found
getEntityGeneric(entity1.Id, entity1);
getEntityGeneric(entity3.Id, entity3);
// Checking if entity2 can not be found
try
{
T foundEntity = null;
foundEntity = dao.GetById(entity2.Id);
Assert.IsNull(foundEntity,
"After deletion entity should not be found with id " + entity2.Id);
}
catch (Exception)
{
Assert.Fail("Should return null if finding the deleted entity");
|
|
}
// Checking if other two entities can still be found in getAll list
List<T> list = getListOfAllEntities();
Assert.IsTrue(list.Contains(entity1),
"After dao method GetAll list should contain entity1");
Assert.IsTrue(list.Contains(entity3),
"After dao method GetAll list should contain entity3");
}
protected abstract void createEntities();
protected abstract void checkAllPropertiesDiffer(T entityToCheck1,
T entityToCheck2);
protected abstract void checkAllPropertiesEqual(T entityToCheck1,
T entityToCheck2);
protected void saveEntitiesGeneric()
{
T savedObject = null;
try
{
dao.SaveOrUpdate(entity1);
savedObject = getPersistentObject(entity1);
Assert.IsNotNull(savedObject,
"DAO method saveOrUpdate should return entity if successfull");
checkAllPropertiesEqual(savedObject, entity1);
entity1 = savedObject;
}
catch (Exception)
{
Assert.Fail("Fail to save entity1");
}
try
{
dao.SaveOrUpdate(entity2);
savedObject = getPersistentObject(entity2);
Assert.IsNotNull(savedObject,
"DAO method saveOrUpdate should return entity if successfull");
checkAllPropertiesEqual(savedObject, entity2);
entity2 = savedObject;
}
catch (Exception)
{
Assert.Fail("Fail to save entity2");
}
try
{
dao.SaveOrUpdate(entity3);
savedObject = getPersistentObject(entity3);
Assert.IsNotNull(savedObject,
"DAO method saveOrUpdate should return entity if successfull");
checkAllPropertiesEqual(savedObject, entity3);
}
catch (Exception)
{
Assert.Fail("Fail to save entity3");
}
}
protected T getPersistentObject(T nonPersistentObject)
{
ICriteria criteria = session.CreateCriteria(typeof(T))
.Add(Example.Create(nonPersistentObject));
IList<T> list = criteria.List<T>();
Assert.IsTrue(list.Count >= 1,
"Count of grups must be equal or more than 1");
return list[0];
}
protected void getEntityGeneric(long id, T entity)
{
T foundEntity = null;
try
{
foundEntity = dao.GetById(id);
Assert.IsNotNull(foundEntity,
"Service method getEntity should return entity if successfull");
checkAllPropertiesEqual(foundEntity, entity);
}
catch (Exception)
{
Assert.Fail("Failed to get entity with id " + id);
}
}
protected List<T> getListOfAllEntities()
{
List<T> list = null;
// Should get not null and not empty list
try
{
list = dao.GetAll();
}
catch (Exception)
{
Assert.Fail(
"Should be able to get all entities that were added before");
}
Assert.IsNotNull(list,
"DAO method GetAll should return list of entities if successfull");
Assert.IsFalse(list.Count == 0,
"DAO method should return not empty list if successfull");
return list;
}
//
public static ISession openSession(String host, int port,
String database, String user, String passwd)
{
ISession session = null;
if (factory == null)
{
FluentConfiguration configuration = Fluently.Configure()
.Database(PostgreSQLConfiguration
.PostgreSQL82.ConnectionString(c => c
.Host(host)
.Port(port)
.Database(database)
.Username(user)
.Password(passwd)))
.Mappings(m => m.FluentMappings.Add<StudentMap>().Add<GroupMap>())
.ExposeConfiguration(BuildSchema);
factory = configuration.BuildSessionFactory();
}
//
session = factory.OpenSession();
return session;
}
//
private static void BuildSchema(Configuration config)
{
new SchemaExport(config).Create(false, true);
}
}
}
9. TestGroupDAO :
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using lab6.domain;
using lab6.dao;
namespace lab6
{
[TestClass]
public class TestGroupDAO:TestGenericDAO<Group>
{
protected IGroupDAO groupDAO = null;
protected Student student1 = null;
protected Student student2 = null;
protected Student student3 = null;
public TestGroupDAO():base()
{
DAOFactory daoFactory = new NHibernateDAOFactory(session);
groupDAO = daoFactory.getGroupDAO();
setDAO(groupDAO);
}
protected override void createEntities()
{
entity1 = new Group();
entity1.GroupName = "KS-091";
entity1.CuratorName = "Pronin P. P.";
|
|
entity1.HeadmanName = "Volosniy R. R.";
entity2 = new Group();
entity2.GroupName = "KS-092";
entity2.CuratorName = "Eroshenko G. L.";
entity2.HeadmanName = "Kruglenko T. R.";
entity3 = new Group();
entity3.GroupName = "KS-093";
entity3.CuratorName = "Grab E. E.";
entity3.HeadmanName = "Stecenko Q R.";
}
protected override void checkAllPropertiesDiffer(Group entityToCheck1,
Group entityToCheck2)
{
Assert.AreNotEqual(entityToCheck1.GroupName, entityToCheck2.GroupName,
"Values must be different");
Assert.AreNotEqual(entityToCheck1.CuratorName,
entityToCheck2.CuratorName, "Values must be different");
Assert.AreNotEqual(entityToCheck1.HeadmanName,
entityToCheck2.HeadmanName, "Values must be different");
}
protected override void checkAllPropertiesEqual(Group entityToCheck1,
Group entityToCheck2)
{
Assert.AreEqual(entityToCheck1.GroupName, entityToCheck2.GroupName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.CuratorName, entityToCheck2.CuratorName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.HeadmanName, entityToCheck2.HeadmanName,
"Values must be equal");
}
[TestMethod]
public void TestGetByIdGroup()
{
base.TestGetByIdGeneric();
}
[TestMethod]
public void TestGetAllGroup()
{
base.TestGetAllGeneric();
}
[TestMethod]
public void TestDeleteGroup()
{
base.TestDeleteGeneric();
}
[TestMethod]
public void TestGetGroupByName()
{
Group group1 = groupDAO.getGroupByName(entity1.GroupName);
Assert.IsNotNull(group1,
"Service method getGroupByName should return group if successfull");
Group group2 = groupDAO.getGroupByName(entity2.GroupName);
Assert.IsNotNull(group2,
"Service method getGroupByName should return group if successfull");
Group group3 = groupDAO.getGroupByName(entity3.GroupName);
Assert.IsNotNull(group3,
"Service method getGroupByName should return group if successfull");
checkAllPropertiesEqual(group1, entity1);
checkAllPropertiesEqual(group2, entity2);
checkAllPropertiesEqual(group3, entity3);
}
[TestMethod]
public void TestGetAllStudentOfGroup()
{
createEntitiesForStudent();
Assert.IsNotNull(student1, "Please, create object for student1");
Assert.IsNotNull(student2, "Please, create object for student2");
Assert.IsNotNull(student3, "Please, create object for student3");
entity1.StudentList.Add(student1);
student1.Group = entity1;
entity1.StudentList.Add(student2);
student2.Group = entity1;
entity1.StudentList.Add(student3);
student3.Group = entity1;
Group savedObject = null;
try
{
dao.SaveOrUpdate(entity1);
savedObject = getPersistentObject(entity1);
Assert.IsNotNull(savedObject,
"DAO method saveOrUpdate should return entity if successfull");
checkAllPropertiesEqual(savedObject, entity1);
entity1 = savedObject;
}
catch (Exception)
{
Assert.Fail("Fail to save entity1");
}
IList<Student> studentList =
groupDAO.getAllStudentOfGroup(entity1.GroupName);
Assert.IsNotNull(studentList, "List can't be null");
Assert.IsTrue(studentList.Count == 3,
"Count of students in the list must be 3");
checkAllPropertiesEqualForStudent(studentList[0], student1);
checkAllPropertiesEqualForStudent(studentList[1], student2);
checkAllPropertiesEqualForStudent(studentList[2], student3);
}
[TestMethod]
public void TestDelGroupByName()
{
try
{
groupDAO.delGroupByName(entity2.GroupName);
}
catch (Exception)
{
Assert.Fail("Deletion should be successful of entity2");
}
// Checking if other two entities can be still found
getEntityGeneric(entity1.Id, entity1);
getEntityGeneric(entity3.Id, entity3);
// Checking if entity2 can not be found
try
{
Group foundGroup = null;
foundGroup = dao.GetById(entity2.Id);
Assert.IsNull(foundGroup,
"After deletion entity should not be found with groupName " +
entity2.GroupName);
}
catch (Exception)
{
Assert.Fail("Should return null if finding the deleted entity");
}
// Checking if other two entities can still be found in getAll list
List<Group> list = getListOfAllEntities();
Assert.IsTrue(list.Contains(entity1),
"After dao method GetAll list should contain entity1");
Assert.IsTrue(list.Contains(entity3),
"After dao method GetAll list should contain entity3");
}
protected void createEntitiesForStudent()
{
student1 = new Student();
student1.FirstName = "Ivanov";
student1.LastName = "Kiril";
student1.Sex = 'M';
student1.Year = 1995;
student2 = new Student();
student2.FirstName = "Petrenko";
student2.LastName = "Ivan";
student2.Sex = 'M';
student2.Year = 1997;
student3 = new Student();
student3.FirstName = "Karpov";
student3.LastName = "Danil";
student3.Sex = 'M';
student3.Year = 2000;
}
protected void checkAllPropertiesEqualForStudent(Student entityToCheck1,
Student entityToCheck2)
{
Assert.AreEqual(entityToCheck1.FirstName, entityToCheck2.FirstName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.LastName, entityToCheck2.LastName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.Sex, entityToCheck2.Sex,
"Values must be equal");
Assert.AreEqual(entityToCheck1.Year, entityToCheck2.Year,
"Values must be equal");
}
}
}
10. StudentDAO
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using lab6.domain;
using lab6.dao;
using NHibernate.Criterion;
using NHibernate;
namespace lab6
{
[TestClass]
public class TestStudentDAO:TestGenericDAO<Student>
{
protected IStudentDAO studentDAO = null;
protected IGroupDAO groupDAO = null;
protected Group group = null;
public TestStudentDAO():base()
{
DAOFactory daoFactory = new NHibernateDAOFactory(session);
studentDAO = daoFactory.getStudentDAO();
groupDAO = daoFactory.getGroupDAO();
setDAO(studentDAO);
}
protected override void createEntities()
{
entity1 = new Student();
entity1.FirstName = "Ivanov";
entity1.LastName = "Kiril";
entity1.Sex = 'M';
entity1.Year = 1995;
entity2 = new Student();
entity2.FirstName = "Petrenko";
entity2.LastName = "Ivan";
entity2.Sex = 'M';
entity2.Year = 1997;
entity3 = new Student();
entity3.FirstName = "Karpov";
entity3.LastName = "Danil";
entity3.Sex = 'M';
entity3.Year = 2000;
}
protected override void checkAllPropertiesDiffer(Student entityToCheck1,
Student entityToCheck2)
{
Assert.AreNotEqual(entityToCheck1.FirstName, entityToCheck2.FirstName,
"Values must be different");
Assert.AreNotEqual(entityToCheck1.LastName, entityToCheck2.LastName,
"Values must be different");
Assert.AreNotEqual(entityToCheck1.Year, entityToCheck2.Year,
"Values must be different");
}
protected override void checkAllPropertiesEqual(Student entityToCheck1,
Student entityToCheck2)
{
Assert.AreEqual(entityToCheck1.FirstName, entityToCheck2.FirstName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.LastName, entityToCheck2.LastName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.Sex, entityToCheck2.Sex,
"Values must be equal");
Assert.AreEqual(entityToCheck1.Year, entityToCheck2.Year,
"Values must be equal");
}
[TestMethod]
public void TestGetByIdStudent()
{
base.TestGetByIdGeneric();
}
[TestMethod]
public void TestGetAllStudent()
{
base.TestGetAllGeneric();
}
[TestMethod]
public void TestDeleteStudent()
{
base.TestDeleteGeneric();
}
[TestMethod]
public void TestGetStudentByGroupFirstNameAndLastName()
{
group = new Group();
group.GroupName = "KS-091";
group.CuratorName = "Pronin P. P.";
group.HeadmanName = "Volosniy R. R.";
group.StudentList.Add(entity1);
entity1.Group = group;
group.StudentList.Add(entity2);
entity2.Group = group;
group.StudentList.Add(entity3);
entity3.Group = group;
Group savedGroup = null;
try
{
groupDAO.SaveOrUpdate(group);
savedGroup = getPersistentGroup(group);
Assert.IsNotNull(savedGroup,
"DAO method saveOrUpdate should return group if successfull");
checkAllPropertiesEqualGroup(savedGroup, group);
group = savedGroup;
}
catch (Exception)
{
Assert.Fail("Fail to save group");
}
getStudentByGroupFirstNameAndLastName(entity1, group.GroupName,
entity1.FirstName, entity1.LastName);
getStudentByGroupFirstNameAndLastName(entity2, group.GroupName,
entity2.FirstName, entity2.LastName);
getStudentByGroupFirstNameAndLastName(entity3, group.GroupName,
entity3.FirstName, entity3.LastName);
group.StudentList.Remove(entity1);
group.StudentList.Remove(entity2);
group.StudentList.Remove(entity3);
entity1.Group = null;
entity2.Group = null;
entity3.Group = null;
groupDAO.Delete(group);
}
protected void getStudentByGroupFirstNameAndLastName(Student student,
string groupName, string firstName, string lastName)
{
Student foundStudent = null;
try
{
foundStudent = studentDAO.getStudentByGroupFirstNameAndLastName(
groupName, firstName, lastName);
Assert.IsNotNull(studentDAO,
"Service method should return student if successfull");
checkAllPropertiesEqual(foundStudent, student);
}
catch (Exception)
{
Assert.Fail("Failed to get student with groupName " +
groupName + " firstName " + firstName+" and lastName " + lastName);
}
}
protected Group getPersistentGroup(Group nonPersistentGroup)
{
ICriteria criteria = session.CreateCriteria(typeof(Group))
.Add(Example.Create(nonPersistentGroup));
IList<Group> list = criteria.List<Group>();
Assert.IsTrue(list.Count >= 1,
"Count of grups must be equal or more than 1");
return list[0];
}
protected void checkAllPropertiesEqualGroup(Group entityToCheck1,
Group entityToCheck2)
{
Assert.AreEqual(entityToCheck1.GroupName, entityToCheck2.GroupName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.CuratorName, entityToCheck2.CuratorName,
"Values must be equal");
Assert.AreEqual(entityToCheck1.HeadmanName, entityToCheck2.HeadmanName,
"Values must be equal");
}
}
}
11. . .
, . 6.2.
. .
6.2
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) | |
(, ) |
- ;
- ;
- ;
- ;
- ( , );
- .
1. ?
2. ?
3. Unit Testing Framework?
4. , , ?
5. Unit Testing Framework , ?
1. . # 2005 .NET 2.0, 3- .: . .- .: , 2007.-1168.
2. . # (2- ).- .: , 2003.-688.
3. C# 2010: .: . . .: . . , 2010.-592.
4. . Microsoft ASP.NET 2.0. . - / . . - .: ; , 2007.- 688.