Skip to content

How to create unit test

Ilfat edited this page May 11, 2017 · 1 revision

How to create unit test

Introduction

Here you find information how to create unit test, where to store unit test

Where

Unit tests are kept in MDBCI_DIRECTORY/spec/unit folder.

How to name tests

Create ruby file like this: CUSTOM_TEST_NAME_spec.rb in unit tests folder

Example of test

Unit test example: https://github.com/OSLL/mdbci/blob/integration/spec/unit/6640_sudo_exit_code_spec.rb
In that test we used some preparation:


before :all do
  $out = Out.new
  $session = Session.new
  $session.mdbciDir = Dir.pwd
  $exception_handler = ExceptionHandler.new
  boxesPath = './BOXES'
  $session.boxes = BoxesManager.new boxesPath
  reposPath = './repo.d'
  $session.repos = RepoManager.new reposPath
end

This, often, is enough to call some function of Session class.

Add tests to test set

If test non parametrized add next line to file MDBCI_DIRECTORY/spec/unit_tasks.rb like:


namespace :run_unit do
  ...
  task :task_CUSTOM_TEST_NAME do |t| RakeTaskManager.new(t).run_unit(PROVIDER_ARGUMENT) end
  ...
end

If test parametrized add next line to file MDBCI_DIRECTORY/spec/unit_parametrized_tasks.rb:


namespace :run_unit_parametrized do
  ...
  task :task_CUSTOM_TEST_NAME do |t| RakeTaskManager.new(t).run_unit_parametrized(PROVIDER_ARGUMENT) end
  ...
end

CUSTOM_TEST_NAME is a name of file with test from unit tests directory but without postfix ‘_spec.rb’
Example of CUSTOM_TEST_NAME:


test file: UNIT_TESTS_DIRECTORY/test1_spec.rb
CUSTOM_TEST_NAME: test1

PROVIDER_ARGUMENT is a ruby array filled with no more that 3 different provider names:

  • DOCKER
  • LIBVIRT
  • PPC

Example of PROVIDER_ARGUMENT:


[DOCKER, LIBVIRT]

Depending on PROVIDER_ARGUMENT, for each provider would be created clone