X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



Comparing Amplitude and Coherence Time Series With ICEYE US GTR Data and ENVI SARscape

Comparing Amplitude and Coherence Time Series With ICEYE US GTR Data and ENVI SARscape

12/3/2025

Large commercial SAR satellite constellations have opened a new era for persistent Earth monitoring, giving analysts the ability to move beyond simple two-image comparisons into robust time series analysis. By acquiring SAR data with near-identical geometry every 24 hours, Ground Track Repeat (GTR) missions minimize geometric decorrelation,... Read More >

Empowering D&I Analysts to Maximize the Value of SAR

Empowering D&I Analysts to Maximize the Value of SAR

12/1/2025

Defense and intelligence (D&I) analysts rely on high-resolution imagery with frequent revisit times to effectively monitor operational areas. While optical imagery is valuable, it faces limitations from cloud cover, smoke, and in some cases, infrequent revisit times. These challenges can hinder timely and accurate data collection and... Read More >

Easily Share Workflows With the Analytics Repository

Easily Share Workflows With the Analytics Repository

10/27/2025

With the recent release of ENVI® 6.2 and the Analytics Repository, it’s now easier than ever to create and share image processing workflows across your organization. With that in mind, we wrote this blog to: Introduce the Analytics Repository Describe how you can use ENVI’s interactive workflows to... Read More >

Deploy, Share, Repeat: AI Meets the Analytics Repository

Deploy, Share, Repeat: AI Meets the Analytics Repository

10/13/2025

The upcoming release of ENVI® Deep Learning 4.0 makes it easier than ever to import, deploy, and share AI models, including industry-standard ONNX models, using the integrated Analytics Repository. Whether you're building deep learning models in PyTorch, TensorFlow, or using ENVI’s native model creation tools, ENVI... Read More >

Blazing a trail: SaraniaSat-led Team Shapes the Future of Space-Based Analytics

Blazing a trail: SaraniaSat-led Team Shapes the Future of Space-Based Analytics

10/13/2025

On July 24, 2025, a unique international partnership of SaraniaSat, NV5 Geospatial Software, BruhnBruhn Innovation (BBI), Netnod, and Hewlett Packard Enterprise (HPE) achieved something unprecedented: a true demonstration of cloud-native computing onboard the International Space Station (ISS) (Fig. 1). Figure 1. Hewlett... Read More >

1345678910Last
12732 Rate this article:
3.7

Unit Testing in IDL

Anonym

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.

Please login or register to post comments.