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!



Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

9/8/2025

Geohazards such as slope instability, erosion, settlement, or seepage pose ongoing risks to critical infrastructure. Roads, railways, pipelines, and utility corridors are especially vulnerable to these natural and human-influenced processes, which can evolve silently until sudden failure occurs. Traditional ground surveys provide only periodic... Read More >

Geo Sessions 2025: Geospatial Vision Beyond the Map

Geo Sessions 2025: Geospatial Vision Beyond the Map

8/5/2025

Lidar, SAR, and Spectral: Geospatial Innovation on the Horizon Last year, Geo Sessions brought together over 5,300 registrants from 159 countries, with attendees representing education, government agencies, consulting, and top geospatial companies like Esri, NOAA, Airbus, Planet, and USGS. At this year's Geo Sessions, NV5 is... Read More >

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

6/3/2025

Rethinking the Reliability of Type 1a Supernovae   How do astronomers measure the universe? It all starts with distance. From gauging the size of a galaxy to calculating how fast the universe is expanding, measuring cosmic distances is essential to understanding everything in the sky. For nearby stars, astronomers use... Read More >

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

5/26/2025

Whether you’re new to remote sensing or a seasoned expert, there is no doubt that large language models (LLMs) like OpenAI’s ChatGPT or Google’s Gemini can be incredibly useful in many aspects of research. From exploring the electromagnetic spectrum to creating object detection models using the latest deep learning... Read More >

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

4/28/2025

When every second counts, the ability to process geospatial data rapidly and accurately isn’t just helpful, it’s critical. Geospatial Intelligence (GEOINT) has always played a pivotal role in defense, security, and disaster response. But in high-tempo operations, traditional workflows are no longer fast enough. Analysts are... Read More >

1345678910Last
«September 2025»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
11419 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.