跳转至

Unit Testing in IDL

原文链接: https://www.nv5geospatialsoftware.com/Learn/Blogs/Blog-Details/unit-testing-in-idl

12741 Rate this article:

3.7

Unit Testing in IDL

Anonym Thursday, May 7, 2015

One of the key aspects of developing any software is testing. Making sure the software behaves as you expect for a variety of inputs is crucial for creating robust and maintainable code. Testing in IDL can be a little tricky and will often require you implement your own system for maintaining tests. However, with a little framework this task can be a lot less daunting.

Before we get into the framework, it would be beneficial to read up on Test Driven Development and the concepts behind writing good tests. There are some great tutorials and overviews on the web and they really help to emphasize the importance of testing. Now, onto the code!

Let’s say we want to write a function CONVERT_TO_STRING. Since we are writing this function from scratch, let’s define the contract of the function. As input, it will take an IDL variable, convert it to a string with a custom format, and return it. Great, let’s write some tests

test_convert_to_string.pro:

pro test_convert_to_string_number

compile_opt idl2

on_error, 2

input = 1

expect = '1'

result = convert_to_string(input)

if result ne expect then begin

message, 'Converting number failed.'

endif

end

pro test_convert_to_string_null

compile_opt idl2

on_error, 2

input = !NULL

expect = '!NULL'

result = convert_to_string(input)

if result ne expect then begin

message, 'Converting number failed.'

endif

end

pro test_convert_to_string_object

compile_opt idl2

on_error, 2

input = hash('a',1,'b',2,'c',3)

expect = '{"c":3,"a":1,"b":2}'

result = convert_to_string(input)

if result ne expect then begin

message, 'Converting number failed.'

endif

end

pro test_convert_to_string

compile_opt idl2

print

print, 'Testing suite for convert_to_string()'

end

Before any code is written we have our test case. The reason we can do this is because we defined the contract of the function. We know exactly what the function should take in as input and what the output should be. Now running this code can be a little tiresome to run so let’s setup some framework.

unit_test_runner.pro:

; Path – path to test directory

pro unit_test_runner, path

compile_opt idl2

if ~file_test(path, /directory) then begin

message, 'Input must be a path.'

endif

test_files = file_search(path, 'test*.pro')

resolve_routine, file_basename(test_files,'.pro'), /compile_full_file

tests = routine_info()

print

print,'--------------------------------------------------------------------------------'

error_count = 0

for i=0, tests.length-1 do begin

catch, errorStatus

if (errorStatus ne 0) then begin

catch, /cancel

print, 'ERROR: ', !ERROR_STATE.msg

i++

error_count++

continue

endif

if (tests[i]).startswith('TEST_') then begin

call_procedure, tests[i]

endif

endfor

print

print,'--------------------------------------------------------------------------------'

print

if error_count gt 0 then begin

print, 'Unit test failures on: ' + path

endif else begin

print, 'Unit tests pass.'

endelse

end

Now all we have to do is give UNIT_TEST_RUNNER a path to our test files and it will run them!  Let’s get busy coding.

convert_to_string.pro:

; Input - IDL Variable

; Output - Custom string representation of the variable

function convert_to_string, var

compile_opt idl2

switch size(var,/TYPE) of

0: begin

return, '!NULL '

break

end

11: begin

if isa(var,'HASH') or isa(var,'DICTIONARY') or isa(var,'ORDEREDHASH') then begin

return, json_serialize(var)

endif

break

end

else: begin

return, strtrim(var,2)

end

endswitch

end

Now let’s run our test suite:


Testing suite for convert_to_string()

% Compiled module: CONVERT_TO_STRING.

ERROR: TEST_CONVERT_TO_STRING_NULL: Converting !NULL failed.


Unit test failures on: C:\convert_to_string

Oops! Our return for the !NULL case isn’t what we are expecting (good thing it’s an easy fix).

By developing software test first you are forced to think about the contract (inputs/outputs) of each function. By implementing unit tests against this contract, we can then use our new function with confidence. If everything has unit tests any problems which arise in the code are easily identified and fixed.

Note: Make sure each code segment is saved to a named file (names are given before the code) and all files are on your IDL PATH.

Landsat Program Well Worth It's Price Tag Use ENVI and DigitalGlobe Data to Help Recovery Efforts in Nepal