# tests module¶

This module collects tools for unit testing the functions in the FreqDemod package. Unit tests were developed using the unittest package. To run the unit tests, open a terminal in the FreqDemod directory and run either:

python -m unittest discover
(or python -m unittest discover --verbose)


or:

python setup.py test


In some of the unit tests, we compare two numpy arrays. In developing this comparison we found the stackoverflow discussion “Comparing numpy float arrays in unit tests” helpful [link].

tests.discover()[source]

Automatically collect all tests in this folder.

tests.main()[source]

Run all tests.

Returns: a unittest.TestResult object

## tests.test_hdf5 module¶

Tests for the hdf5 module. In writing units tests, it is crucially important to remember that the unittest object is recreated every function call [see here]. You thus cannot expect, say, self.f to retain a value if it is passed between functions. You cannot pass a value between functions this way if your object is a unittest object.

You can pass values from the setUp function to other functions, because the setUp function is called once before every other function in the class. Likewise the tearDown function.

There is a way to pass data between functions in a unittest onject. This requires use of the special setUpClass, illustrated below in the object Test_disk_data2. This test mimics how we would like to use HDF5 data: open file on disk, pass the data between object methods, reading and writing the data as needed.

• h5ls command line tool [link]
class test_hdf5.Test_update_attrs(methodName='runTest')[source]

Bases: unittest.case.TestCase

Test the helper function update_attrs

filename = '.Test_update_h5_attrs.h5'
setUp()[source]
test_overwrite()[source]

The function should overwrite existing attributes

test_normal_write()[source]

Writing a non-empty dictionary should do something

test_empty_write()[source]

Writing an empty dictionary should do nothing.

tearDown()[source]

Close the h5 file, and remove the file for the next iteration.

class test_hdf5.Test_memory_data(methodName='runTest')[source]

Bases: unittest.case.TestCase

filename = '.Test_update_h5_data.h5'
setUp()[source]
test_array_read()[source]

Open in memory, write, read array, close

test_attr_read()[source]

Open in memory, write, read attribute, close

tearDown()[source]

Close the h5 file, and remove the file for the next iteration.

class test_hdf5.Test_disk_data(methodName='runTest')[source]

Bases: unittest.case.TestCase

Write to disk, open, read, close; Write to disk, open, read, close; etc

filename = '.Test_update_h5_data.h5'
setUp()[source]
test_01_read()[source]

Open file from disk, read array, close

test_02_read()[source]

Open file from disk, read attribute, close

tearDown()[source]

Remove the file for the next iteration.

class test_hdf5.Test_disk_data2(methodName='runTest')[source]

Bases: unittest.case.TestCase

Write once to disk, open once from disk, read, write, read, close. Leave the file intact to we can examine it by calling:

h5ls -v .Test_disk_data2.h5

classmethod setUpClass()[source]
test_01_read()[source]

test_02_read()[source]

test_03_write()[source]

Write new attribute

test_04_read()[source]

classmethod tearDownClass()[source]
class test_hdf5.Test_update_attrs_extended(methodName='runTest')[source]

Bases: unittest.case.TestCase

Read and write an x and y dataset representing a cantilever oscillation. Use an HDF5 file format that Dwyer, Marohn, and Harrell have agreed upon (with minor modifications). This class’s unit tests create a hidden HDF5 whose contents can be examined using the command-line call:

h5ls -v .Test_update_attrs_extended.h5


The function test_02_read reads the h5 file and prints out its contents. The outputs of print statements are usually swallowed during the unit test. To display the informative print statements, you can initiate unit testing using the following command:

nosetests -sv

filename = '.Test_update_attrs_extended.h5'
setUp()[source]

“Record the date and time for use by the functions below. Delete the output file if it exists so we can make it anew.

write_h5file()[source]

Write the representative dataset to a file.

test_read()[source]

Read the representative dataset from disk, and compare with the data / attributes written.

tearDown()[source]

## tests.test_freqdemod module¶

Tests for the demodulate module.

class test_freqdemod.InitLoadSaveTests(methodName='runTest')[source]

Bases: unittest.case.TestCase

Make sure the Signal object is set up correctly.

filename = '.InitLoadSaveTests_1.h5'
setUp()[source]

Create an trial Signal object

test_report()[source]

Initialize Signal object

test_x()[source]

Check x-array data.

test_y()[source]

Check y-array data.

tearDown()[source]

Close the h5 files before the next iteration.

class test_freqdemod.TestClose(methodName='runTest')[source]

Bases: unittest.case.TestCase

filename = '.TestClose.h5'
setUp()[source]
tearDown()[source]
test_close()[source]

Verify closed object by testing one of the attributes

class test_freqdemod.MaskTests(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]

Create a trial Signal object

test_binarate_1()[source]

Binarate mask middle; test length is 2^n

test_binarate_2()[source]

Binarate mask start; test length is 2^n

test_binarate_3()[source]

Binarate mask end test length is 2^n

test_binarate_4()[source]

If we have not called binarate, then workup/time/mask/binarate does not exist

test_binarate_5()[source]

If we have called binarate, then workup/time/mask/binarate does exist

tearDown()[source]

Close the h5 files before the next iteration.

class test_freqdemod.FFTTests(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]

Create an trial Signal object

testfft_1()[source]

FFT: test that the resulting data is complex

testfft_2()[source]

FFT: test that the complex Hilbert transform filter is real

testfft_3()[source]

FFT: test the complex Hilbert transform filter near freq = 0

test_phase_fit_rounding()[source]
tearDown()[source]

Close the h5 files before the next iteration.

class test_freqdemod.FFTOddPoints(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]
test_ifft_odd_pts()[source]
tearDown()[source]
class test_freqdemod.HDF5LoadGeneral(methodName='runTest')[source]

Bases: unittest.case.TestCase

filename = '.general_format_h5_file.h5'
setUp()[source]
test_load_general_format_h5_x_y()[source]
test_load_general_format_h5_y_dt()[source]
test_load_general_no_x_or_dt_specified()[source]
tearDown()[source]
class test_freqdemod.HDF5LoadDefault(methodName='runTest')[source]

Bases: unittest.case.TestCase

filename = '.default_format_h5_file.h5'
setUp()[source]
test_hdf5_general_all_attrs_specified()[source]
test_hdf5_general_infer_dt()[source]
test_hdf5_general_infer_missing_label()[source]
test_hdf5_general_infer_missing_abscissa()[source]
test_hdf5_general_infer_missing_y_labels()[source]
tearDown()[source]
class test_freqdemod.SaveTests(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]
test_save_pass_list_datasets()[source]
test_save_pass_string()[source]
tearDown()[source]
class test_freqdemod.SaveBeforeWorkupTest(methodName='runTest')[source]

Bases: unittest.case.TestCase

setUp()[source]
test_save_before_workup()[source]
tearDown()[source]
class test_freqdemod.MiscTests(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_array_middle_1()[source]

Misc: mean time (n odd)

test_array_middle_2()[source]

Misc: mean time (n even)