...
Code Block |
---|
. ├── fruit │ └── fruit.f90 ├── mod ├── src │ └── <your_module>.f90 └── test ├── <your_module>_test.f90 └── <yourfruit_driver_name>.f90 |
- 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.
- <yourfruit_driver_name>.f90 - file with test driving program. Filename could be anything, just remember it so you can use it while compiling and running projectYou can change this file name, but remember to modify Makefile, so it could find it.
Install requirements
setup fruit library
Download fruit.f90 https://github.com/mortele/FRUIT/blob/master/src/fruit.f90 into .<project_dir>/fruit directory.
Code Block |
---|
cd <project_dir>/fruit wget https://raw.githubusercontent.com/mortele/FRUIT/master/src/fruit.f90 |
NOTE: While compiling fruit.f90 an error "Logicals must be compared with .eqv. instead of .eq." may occure. In this case you will need to modify fruit.f90 lines 912 and 913 editing ".eq." to ".eqv.".
Create project files
Create file with unit tests
...
Create unit test driver
Create file named <yourfruit_driver_name>.f90. It will call your tests and handle fruit init and summary. You will need to modify it everytime you add new tests.
Code Block |
---|
!file: <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 |
...
Code Block |
---|
!file: <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 |
Finalization
Create Makefile
It is a build instruction that makes compiling your project easy.
To use this code without modification you will need to follow convention:
- Put your tests in /test directory and name them *_test.f90
- Name your main program main.f90
- Name your test driver "fruit_driver.f90"
Otherwise you will need to modify some of it's lines.
Code Block |
---|
#file: <project_dir>/Makefile
C=gfortran -O3
FFLAGS=-c -J ./mod
OBJS=$(patsubst %.f90,%.o,$(wildcard src/*.f90))
TESTED_OBJS=$(filter-out src/main.o, $(OBJS))
TESTS=$(patsubst %.f90,%.o,$(wildcard test/*test.f90))
LIB=$(patsubst %.f90,%.o,$(wildcard fruit/*.f90))
DRIVER_SOURCE=test/fruit_driver.f90
DRIVER_OBJ=$(patsubst %.f90,%.o, $(DRIVER_SOURCE))
all: run
run: $(OBJS)
@echo "Building executable"
$(FC) $(OBJS) -o $@
$(OBJS): src/%.o: src/%.f90
@echo "Compiling objects"
$(FC) $(FFLAGS) $< -o $@
test: fruit/fruit.o $(TESTED_OBJS) $(TESTS) $(DRIVER_OBJ)
@echo "Building executable"
$(FC) $^ -o run_test
$(TESTS): test/%.o: test/%.f90
@echo "Compiling tests"
$(FC) $(FFLAGS) $< -o $@
$(DRIVER_OBJ): $(DRIVER_SOURCE)
@echo "Compiling driver"
$(FC) $(FFLAGS) $< -o $@
fruit/fruit.o: fruit/fruit.f90
@echo "Compiling FRUIT library"
$(FC) $(FFLAGS) $< -o $@
clean:
rm mod/*.mod -f
rm src/*.o -f
rm test/*.o -f
rm fruit/*.o -f
rm run -f
|
Run tests
Code Block |
---|
cd <project_dir>
#to compile and run tests
make test
./run_test
#to clean after build
make clean
#to compile and run you program without tests
make
./run |
Using Using setup and teardown subroutines
NOTE: This step is optional and is needed to be done only if you want to use setup and teardown subroutines.
NOTE: Using setup and teardown subroutines with this library is complex and unellegant. Consider using another framework or FRUIT+Ruby version.
Add setup and teardown subroutines
...
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. Just put it into .sh file and run.
Modify Makefile
Add lines presented below at the end of the Makefile.
Code Block |
---|
test/fruit_basket.o: test/fruit_basket.f90
@echo "Compiling FRUIT basket file"
$(FC) $(FFLAGS) $< -o $@ |
Also you need to modify this line:
Code Block |
---|
test: fruit/fruit.o $(TESTED_OBJS) $(TESTS) $(DRIVER_OBJ) |
as follows:
Code Block |
---|
test: fruit/fruit.o $(TESTED_OBJS) $(TESTS) test/fruit_basket.o $(DRIVER_OBJ) |
Code Block |
#filename: build_run_and_clean.sh
#create mod directory if not exists
mkdir -p mod
#compile source files
# -J specifies where .mod files will be generated, and add this path in compiler search location
gfortran -c ./src/*.f90 -J ./mod
gfortran -c ./fruit/*.f90 -J ./mod
gfortran -c ./test/*.f90 -J ./mod
# create executable file
gfortran -o fruit_driver *.o
#run tests
./fruit_driver
#clean
find . -name "*.o" -type f -delete
find . -name "*.mod" -type f -delete |
Example output
Output created with project described above
...