Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • fruit.f90 - fruit source code
  • mod - directory used to store .mod files
  • <your_module>.f90 - your actual module
  • <your_module>_test.f90 - file with unit tests for your module
  • <your_driver_name>.f90 - file with test driving program. Filename could be anything, just remember it so you can use it while compiling and running project.

...

 Install requirements

setup fruit

Download fruit.f90 https://github.com/mortele/FRUIT/blob/master/src/fruit.f90 and put it into ./fruit directory.

...

Create project files

Create file with unit tests

Create file named <your_module>_test.f90 and put it into ./test directory.

Your module may looks like:This is you file that contains unit tests for your program.

Code Block
!filenamefile: <project_dir>/test/calculator_test.f90  

module calculator_test
use fruit

contains
subroutine test_calculator
	use calculator
	integer :: result

	!test add subroutine
	call add (2,2,result)
	call assert_equals(4,result)

end subroutine test_calculator
end module calculator_test

Create unit test driver

Create file named <your_driver_name>.f90 and put it into ./test directory.test driver is actual program that runs tests. It will call your tests and handle fruit init and summary.

Code Block
!filenamefile: <project_dir>/test/fruit_driver.f90

program fruit_driver
	use fruit
	use calculator_test
	call init_fruit

	!if you want to generate XML result
	!call init_fruit_xml

	call test_calculator

	!call fruit_summary_xml
	call fruit_summary
end program fruit_driver

 Create your module

...

Create file named <your_module>.f90 and put it into ./scr directory. This file will be tested by module created in step 3.1.

Code Block
!filenamefile: <project_dir>/src/calculator.f90

module calculator
implicit none
contains
	subroutine add(a,b,output)
		integer, intent (in) :: a,b
		integer, intent (out) :: output
		output=a+b
	end subroutine add

end module calculator

...

 Add setup and teardown subroutines

--TODO--First you need to define setup and teardown routines.

Code Block
!filenamefile: <project_dir>/test/calculator_test.f90

module calculator_test
use fruit

contains
subroutine setup
	!...
end  subroutine setup
	!...
subroutine teardown
	!...
end  subroutine teardown

subroutine test_calculator
	use calculator
	integer :: result

	!test add subroutine
	call add (2,2,result)
	call assert_equals(4,result)

end subroutine test_calculator
end module calculator_test

Create basket file

Basket file handles setup and teardown subroutines calls and also captures responsibility of running tests from test_driver module. It should be created in ./test directory. 

Code Block
!file: <project_dir>/test/fruit_basket.f90

module fruit_basket
  use fruit
contains
  subroutine calculator_test_all_tests
    use calculator_test

    call setup
    write (*, *) "  ..running test: test_calculator"
    call set_unit_name('test_calculator')
    call run_test_case (test_calculator, "test_calculator")
    if (.not. is_case_passed()) then
      write(*,*) 
      write(*,*) '  Un-satisfied spec:'
      write(*,*) '  -- calculator'
      write(*,*) 
      call case_failed_xml("test_calculator", &
      & "calculator_test")
    else
      call case_passed_xml("test_calculator", &
      & "calculator_test")
    end if
    call teardown

  end subroutine calculator_test_all_tests

  subroutine fruit_basket
    call calculator_test_all_tests
  end subroutine fruit_basket

end module fruit_basket


Pay attention to code lines presented below. They come from code above. You can use them to call tests from desired test module.

Code Block
call set_unit_name('test_calculator')
call run_test_case (test_calculator, "test_calculator")

Modify test driver

Now test subroutines calls are moved from test_driver to test_basket. You need to import test_basket module and call its main subroutine. In this case it is "fruit_basket" subroutine.

Code Block
!file: <project_dir>/test/fruit_driver.f90

program fruit_driver
	use fruit
	use fruit_basket
	call init_fruit

	call fruit_basket

	call fruit_summary
end program fruit_driver

Run tests

This bash code will compile all source codes and run tests described in ./test/calculator_test.f90 file.

...