Testing
-------

All of the following `make` commands run the tests in the currently active
Python environment, and need to be invoked in the Git repo work directory.

By default, the tests use the `pywbem` and `pywbem_mock` modules from the
respective directories in the Git repo work directory.
Pywbem 0.14.5 introduced a way to test installed versions of the pywbem
package. For details, see section "Testing installed versions of pywbem".

The `tests` directory has the following subdirectory structure:

    tests
     +-- unittest            Unit tests
     |    +-- utils               Utility functions used by unit tests
     |    +-- pywbem              Unit tests for the pywbem package
     |    +-- pywbem_mock         Unit tests for the pywbem_mock package
     |    +-- unittest_utils      Unit tests for tests/unittest/utils
     |    +-- functiontest        Unit tests for tests/functiontest
     |    +-- end2endtest_utils   Unit tests for tests/end2endtest/utils
     |    +-- servers             Unit tests for tests/servers
     +-- functiontest        Function tests
     +-- end2endtest         End2end tests
     |    +-- utils               Utility functions used by end2end tests
     +-- manualtest          Manual tests
     +-- server_definitions  WBEM server definition file used by some tests and module
     |                         for accessing it
     +-- profiles            Simple definitions of management profiles used by some tests
     +-- schema              The CIM schema MOF files used by some MOF tests
     +-- dtd                 The CIM DTD file used by some CIM-XML validation tests

There are multiple types of tests in pywbem:

1. Unit tests and function tests

   These tests do not require any WBEM server to be available, and the tests
   validate their results automatically.

   The distinction between unit tests and function tests as used in pywbem is
   that function tests exercise the entire pywbem client component or entire
   pywbem scripts, while unit tests exercise single modules.

   They are run by executing:

       $ make test

   Test execution can be modified by a number of environment variables, as
   documented in the make help (execute `make help`).

2. End2end tests

   These tests are run against one or more WBEM servers, and the tests validate
   their results automatically.

   They are run by preparing a server definition file:

       tests/server_definitions/server_definition_file.yml

   from the provided example, and by executing:

       $ make end2end

   Again, test execution can be modified by a number of environment variables,
   as documented in the make help (execute `make help`).

3. Manual tests

   There are several Python scripts and shell scripts that can be run manually.
   The results need to be validated manually.

   These scripts are in the directory:

       tests/manualtest/

   and are executed by simply invoking them from within the main directory
   of the repository, and providing that main directory as a Python package
   path, e.g.:

       PYTHONPATH=. tests/manualtest/run_cim_operations.py

   Some of the scripts support a `--help` option that informs about their
   usage.

   The `run_cim_operations.py` script needs a particular MOF file loaded in the
   repository of the WBEM server that is used for the test. This can be done
   using the MOF compiler of pywbem:

       $ mof_compiler -s <target_url> tests/unittest/pywbem/test.mof

To run the unit and function tests in all supported Python environments, the
Tox tool can be used. It creates the necessary virtual Python environments and
executes `make test` (i.e. the unit and function tests) in each of them.

For running Tox, it does not matter which Python environment is currently
active, as long as the Python `tox` package is installed in it:

    $ tox                              # Run tests on all supported Python versions
    $ tox -e py38                      # Run tests on Python 3.8


Testing installed versions of pywbem
------------------------------------

By default, the tests use the pywbem and pywbem_mock modules from the
respective directories in the Git repo work directory.

Pywbem 0.14.5 introduced a way to test installed versions of the pywbem
package. This is useful for example for testing a version of pywbem that has
been packaged as an OS-level package. Typically, such a version would be
installed into the system Python.

In order to not clutter up the system Python with Python packages needed for
running the pywbem tests, the following steps use a virtual Python environment
that uses the packages of the system Python. That way, the installed version of
pywbem becomes available to the virtual Python environment from the system
Python, while any additional packages that are needed but not yet available
that way, will be installed into the virtual Python environment.

Follow these steps to run pywbem tests against a version of pywbem that is
installed into the system Python:

1. Verify that the following commands are available when the system Python
   is active:

       $ virtualenv --version   # Python virtualenv package
       $ pip --version

2. Create and activate a virtual Python environment of the intended Python
   version, that is based on the system Python:

       $ virtualenv --system-site-packages .virtualenv/test
       $ source .virtualenv/test/bin/activate

   The pywbem project is set up so that Git ignores the ``.virtualenv``
   directory, so use that directory name for ease of Git handling.

3. Verify that in that virtual Python environment, pywbem comes from the
   intended installation:

       $ pip show pywbem

4. Ensure a fresh start of the make process. This should be done whenever
   switching between the installed version of pywbem and the local directories:

       $ make clobber

5. Run the pywbem tests with environment variable ``TEST_INSTALLED`` being set:

       $ TEST_INSTALLED=1 make test

   This will assume that the pywbem package and any prerequisite Python
   packages and OS-level packages are already installed.

   This will also move the current directory (i.e. the repo working directory)
   to the end of the module search path, so that the installed version of
   pywbem is used when importing it into the test scripts.

   Setting ``TEST_INSTALLED=DEBUG`` causes some debug messages to be printed
   that allow verifying from where the pywbem and pywbem_mock modules
   are loaded.

   This also works for the pywbem end2end tests:

       $ TEST_INSTALLED=1 make end2end

Note that tox does not support creating its virtual Python environments
based on the system Python, so at this point, tox cannot be used for this
approach.
