# 24. Unit test support

## `sasmodels.model_test`

Run model unit tests.

Usage:

    python -m sasmodels.model_test [opencl|cuda|dll|all] model1 model2 ...

If model1 is ‘all’, then all except the remaining models will be tested. Subgroups are also possible, such as ‘py’, ‘single’ or ‘1d’. See `core.list_models()` for details.

Each model is tested using the default parameters at q=0.1, (qx, qy)=(0.1, 0.1), and Fq is called to make sure R_eff, volume and volume ratio are computed. The return values at these points are not considered. The test is only to verify that the models run to completion, and do not produce inf or NaN.

Tests are defined with the tests attribute in the model.py file. tests is a list of individual tests to run, where each test consists of the parameter values for the test, the q-values and the expected results. For the effective radius test and volume ratio tests, use the extended output form, which checks each output of kernel.Fq. For 1-D tests, either specify the q value or a list of q-values, and the corresponding I(q) value, or list of I(q) values.

That is:

    tests = [
        [ {parameters}, q, I(q)],
        [ {parameters}, [q], [I(q)] ],
        [ {parameters}, [q1, q2, ...], [I(q1), I(q2), ...]],

        [ {parameters}, (qx, qy), I(qx, Iqy)],
        [ {parameters}, [(qx1, qy1), (qx2, qy2), ...],
                        [I(qx1, qy1), I(qx2, qy2), ...]],

        [ {parameters}, q, F(q), F^2(q), R_eff, V, V_r ],
        ...
    ]

Parameters are key:value pairs, where key is one of the parameters of the model and value is the value to use for the test. Any parameters not given in the parameter list will take on the default parameter value.

Precision defaults to 5 digits (relative).

sasmodels.model_test.check_model(model_info: ModelInfo) → tuple[bool, str]

  Run the tests for a single model, capturing the output.

  Returns success status and the output string.

sasmodels.model_test.invalid_pars(partable: ParameterTable, pars: dict[str, float]) → list[str]

  Return a list of parameter names that are not part of the model.

sasmodels.model_test.is_near(target: float, actual: float, digits: int = 5) → bool

  Returns true if actual is within digits significant digits of target.

  taget zero and inf should match actual zero and inf. If you want to accept eps for zero, choose a value such as 1e-10, which must match up to +/- 1e-15 when digits is the default value of 5.

  If target is None, then just make sure that actual is not NaN.

  If target is NaN, make sure actual is NaN.

sasmodels.model_test.main() → int

  Run tests given is models.

  Returns 0 if success or 1 if any tests fail.

sasmodels.model_test.make_suite(loaders: list[str], models: list[str]) → TestSuite

  Construct the pyunit test suite.

  loaders is the list of kernel drivers to use (dll, opencl or cuda). For python model the python driver is always used.

  models is the list of models to test, or [“all”] to test all models.

sasmodels.model_test.model_tests() → Iterator[Callable[[], None]]

  Test runner visible to nosetests.

  Run “nosetests sasmodels” on the command line to invoke it.

sasmodels.model_test.run_one(model_name: str) → str

  [Deprecated] Run the tests associated with model_name.

  Use the following instead:

      succss, output = check_model(load_model_info(model_name))