Skip to content

Unit testing suites

Jeff Krzywon edited this page Oct 27, 2022 · 9 revisions

Unit tests in SasView

Two unit test suites exist within SasView; one to test the calculations performed in the sascalc package and another to test the GUI behavior in the qtgui package. The sascalc unit tests are housed in the test package in the base directory of SasView. The GUI unit tests are housed within each subpackage of the qtgui package in a directories named UnitTesting.

A successful sasview build requires the sascalc unit tests are successful. Currently, the qtgui tests are checked during the build, but not all are currently passing. Work is underway to fix this issue and can be tracked with issue #1732.

Any new GUI or calculation features should come with unit tests and all new tests must pass. Any existing tests that are currently succeeding should continue to pass. Fixing existing, failing unit tests while working on the same or similar part of code is encouraged.

For more information on unit testing, please see the wikipedia page on unit tests.

Calculation tests

Unit tests for the sascalc package are held within the /test/ directory in the base directory of the sasview repository. Each subdirectory in the test directory houses tests specific to each subpackage in sascalc. There are two scripts available to run the unit tests, one that runs the entire test suite and another that runs tests from a specific test file.

  • To run all tests from the base sasview directory: python test\utest_sasview.py
  • To run a single test package from the base sasview directory: python test\run_one.py <test_package>. The test_package can be either an absolute path to a python file or a relative path from the location the script was run.

Adding Calculator tests

The test runner, utest_sasview.py, recursively steps through all subdirectories in the /tests/ directory and looks for any files that match the name pattern utest_<test_name>.py and runs them as python files. Individual files use the unittest package to run their respective unit tests.

To add a new test to an existing python file, simply add a new method starting with the word test in any class that inherits from unittest.TestCase. To add a new file with tests, import unittest, create a class that inherits from unittest.TestCase, add a method to the class that starts with the word test, and add if name == '__main__': unittest.main() at the end of the file. If you would like to run a series of actions before and/or after each test runs, the TestCase class has a setUp method that will run before each test and a tearDown method that will run after each test completes.

  • Importing the unittest package: import unittest
  • Adding a new class of unit tests: class <testClassName>(unittest.TestCase):
  • Adding a new test to an existing class: def test_<test_name>(self):
  • Ensure the test runner runs all tests in the file: if name == '__main__': unittest.main()
  • Run a series of actions before each test starts: def setUp(self):
  • Run a series of actions after each test completes: def tearDown(self):

GUI tests

As of October 25, 2022, GUI test operations have changed. An updated guide is in the works.

Unit tests for the qtgui package are housed within each subpackage and should be specific to that subpackage, e.g. the fitting GUI tests are held in the Perspectives/Fitting/UnitTesting directory. The main test runner, GUITests.py, can accept a list of arguments to run specific tests or will run all tests if no argument is given.

In order to run the GUI tests, both sasview and sasmodels must be in your python path. To do this, run setup.py with the install argument for both packages: python setup.py clean build install. Any changes to a test file must be present in the python path for the runner to pick it up. To accomplish this, you can either run setup.py after every change, or you can symbolically link your sasview repository into your Python site-packages folder.

Adding symbolic links by operating system: * Windows: mklink /D <path.to.python.site-packages>\sasview <path.to.sasview> * MacOS and Linux: ln -s <path.to.sasview> <path.to.python.site-packages>\sasview

Running unit tests: * To run all unit tests: python src\sas\qtgui\GUITests.py * To run a subset of tests: python src\sas\qtgui\GUITests.py <suiteName1> ... <suitNameN> where suiteName is one of these options. Any number of suites can be given:

| suiteName | Description of the tests | Location of unit tests | | ------------- | ---------------------------- | -------------------------- | | calculatorsSuite | Tests related to the Keissig, density, generic scattering, SLD, slit size, and resolution calculators. | src\sas\qtgui\Calculators\UnitTesting | | corfuncPerspectiveSuite | Tests related to the corfunc perspective. | src\sas\qtgui\Perspectives\Corfunc\UnitTesting | | fittingSuite | Tests related to the fitting perspective, including basic fits, constrained fits, and the interactions between pages. | src\sas\qtgui\Perspectives\Fitting\UnitTesting | | invariantPerspectiveSuite | Tests related to the invariant perspective. | src\sas\qtgui\Perspectives\Invariant\UnitTesting | | inversionPerspectiveSuite | Tests related to the P(r) inversion perspective. | src\sas\qtgui\Perspectives\Inversion\UnitTesting | | mainSuite | Tests related to the main window, drop-down menus, data explorer, about box, and welcome panel. | src\sas\qtgui\MainWindow\UnitTesting | | plottingSuite | Tests related to 1D and 2D plots, including slicers and box sums, manipulations, and modifications made to the plots. | src\sas\qtgui\Plotting\UnitTesting | | utilitiesSuite | Tests related to common GUI elements including the data model, model editor, file converter, and batch output windows. | src\sas\qtgui\Utilities\UnitTesting |

### Adding GUI tests

The test runner, GUITests.py, imports all unit tests at run-time for the suite(s) specified as arguments. New test files must be imported within the test runner and added to each suite that file should be run within. The import should be relative to the test runner.

Test files should be stored within the UnitTesting package within the respective package the test is meant for. Similar to the calculation tests, the GUI tests use the unittest package and use many of the same paradigms, but each test file must be explicitly imported within the runner file and the test file must be explicitly added to a test suite.

* Excluding the test runners, the information in calculation tests also applies to the GUI tests. * Importing the test file in GUITests.py: from <relative.package.location> import <testFile> * Adding a new suite or individual test file to an existing suite in GUITests.py: * def suiteName(): * suites = ( * unittest.makeSuite(<testFile>.<testClass>, 'test'), * ) * return unittest.TestSuite(suites) * When adding a new test suite, be sure to add the <suiteName> to the ALL_SUITES list as a string: ALL_SUITES = ['suiteName1', ..., 'suiteNameN']

Clone this wiki locally