In this example we will execute some automatic tests for Python code implementing the [power iteration method]. This is an iterative algorithm for finding an approximation for the largest (in absolute value) eigenvalue
The power iteration method is implemented in Python by makeing use of the [Numpy] library. The code can be found in power_method.py
. For details of the implementation please refer to the source file.
The tests are provided in the file test_power_method.py
. The tests rely on test [Pytest] test framework.
Two integration tests are implemented:
-
Test the final result for the eigenvalue when using the eigenvalues to abort the iterations. The function
def test_real_symmetric_iter_eigenvalues(matrix_file): # ...
implements this test.
-
Test the final result for the eigenvalue when using the eigenvectors to abort the iterations. The function
def test_real_symmetric_iter_eigenvector(matrix_file): # ...
implements this test.
Both tests use real-valued symmetric matrices test_data
.
Make sure to have Python version >=3.9.x
installed on your system. If you do not have a corresponding Python version already installed you can install it with e.g. [Miniconda].
Use on the provided files requirements.txt
or environment.yml
to set up a Python [virtual environment] or an [Anaconda environment].
Open a terminal emulator and make sure Python is contained in the PATH
:
$ which python3
/usr/bin/python3
Next, execute the following commands to establish the Python environment:
$ mkdir -vp pyenv # Host venv in current workdir
$ python3 -m venv pyenv # Create venv
$ source pyenv/bin/activate # Acivate venv
$ pip install --upgrade pip && pip install -r requirements.txt
Open a terminal emulator and make sure the conda
utility is in the PATH
. This should be the case if you have installed either [Miniconda3] or [Anaconda3]. Next, establish the Anaconda environment by running the following commands from the shell prompt:
$ conda env create --file environment.yml
This will create an environment named AutomaticTesting
which can be activated like so:
$ source activate AutomaticTesting
Pytest is needed to execute the tests. Hence make sure it has been properly installed:
$ python -c "import pytest; print(pytest.__version__)"
This should print the Pytest version number (7.1.2 in my case).
Running the test is as simple as
$ pytest test_power_method.py # Add -v for more verbose output.
If you just want to execute the tests for the eigenvalue criterion for aborting the iterations use:
$ pytest -k eigenvalues test_power_method.py
The pattern eigenvalues
will make Pytest select only the test function with a name matching this pattern. Conversely, if you want to limit yourself to the tests for the eigenvector criterion use the eigenvectors
pattern:
$ pytest -k eigenvectors test_power_method.py