Y
Y
Ytsu Ytsuevich2015-02-01 01:25:41
Software testing
Ytsu Ytsuevich, 2015-02-01 01:25:41

How to test classes correctly?

Wrote a class to query data from the database.
Let 's say a student's name and age .
Data is coming, how can I test it, maybe it’s not right!
Create an additional base?
PS The stump is clear, the task is trivial, for example, in my case, the data is taken from 3 tables

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-02-01
@EvilsInterrupt

1.
Data is coming, how can I test it, maybe it’s not right!
To do this, you need to ask yourself the question "What kind of data will be wrong?" and rewrite them on a piece of paper or remember and keep in mind.
2.
>>Create additional base?
Not! For a class that works with the base, write refactoring. The essence of refactoring is to redesign to inherit this class working with the database from some interface. Further from the interface, generate several classes of descendants for each situation described in paragraph 1. these will be mocks, or rather State-classes.
Этот вид тестирования называется "State-based testing". Основан на том что заранее пишутся классы имеющее вполне заранее определенное и конкретное состояние, как правило захардкоженное(В data driven testing пока не вдавайтесь, ибо увязнете в деталях). Такие классы нужно писать как можно проще !!! Чтобы в будущем удалять код не было так жалко )))
3)
В тест-методах , т.е. в юнит-тестах вызываете production-код,но подставляете не реальную БД, а классы написанные п.2. и проверяете состояние.
Попробую написать простейший псевдокод, сходство с конкретным языком чисто случайно:

interface StudentDataBase:
  getName()
  getAge()

// production code
class OracleDataBase implements StudentDataBase
  getName()
  getAge()

// For Testing goal
class BadNameCorectAge implements StudentDataBase
  getName() {
    return 'Wrong-name'
  }
  getAge() {
    return 19;
  }

TEST(WithBadNameForStudent) {
   // [1] Подготовительная часть. Т.е. 'Arrange' из паттерна Arrange-Act-Assert
    BadNameCorectAge database;
    // Здесь с псевдо-базой используется код из production-части, который вы хотите проверить
    TestObject testObj;
    testObj->setDataBase(database);

   // Выполняем действие в production-части и тут же проверяем 
  // [2] Другими словами это совмещенная Act-Assert из паттерна ArrangeAct-Assert
    EXPECT_EXCEPTION(testObj->process(), WrongNameException);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question