Creating Tests

GS Quant uses pytest to run unit tests across all packages. We track coverage across the project using pytest-cov to manage the health of the project. Some general guidelines for testing in GS Quant:

  • Organization tests live in the /test folder, where they follow the package structure of GS Quant. Please add a new folder to the tests folder if you are adding a new package to GS Quant
  • Unit Tests unit tests for Python modules should be runnable by any developer, without needing an API key. Mock API responses to ensure other developers can run all relevant tests
  • Mocking our tests use [pytest-mock] in order to simulate valid responses
  • Coverage: certain packages have 100% test coverage enforced, for example the Timeseries Package. If submitting a pull request, please follow coverage guidelines

Writing Tests

To create a new test, add your file to the relevant folder in /tests. A simple test function will look something like this:

import pytest
from pandas.util.testing import assert_series_equal
from gs_quant.timeseries import *

def test_returns():
    dates = [
        date(2019, 1, 1),
        date(2019, 1, 2),
        date(2019, 1, 3),
        date(2019, 1, 4),
        date(2019, 1, 5),
        date(2019, 1, 6),

    x = pd.Series([])
    assert_series_equal(x, returns(x))

    x = pd.Series([100.0, 101, 103.02, 100.9596, 100.9596, 102.978792], index=dates)

Running Tests

Tests are located in gs_quant/test. To run tests you will need to configure pytest and then run as follows:

gs_quant>pytest gs_quant/test

To run tests on a specific folder, simply specify the folder name as follows:

gs_quant>pytest gs_quant/test/timeseries/

You can also run tests on a specific file:

gs_quant>pytest gs_quant/test/timeseries/

Output will look something like this:

======================================= test session starts =======================================
platform win32 -- Python 3.7.0, pytest-5.3.2, py-1.8.1, pluggy-0.13.1
rootdir: H:\gs_quant_2\gs_quant
plugins: cov-2.8.1, mock-1.13.0
collected 13 items

H:\gs_quant_2\gs_quant .............                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        [100%]

======================================= 13 passed in 2.77s =========================================

Test Coverage

We use pytest-cov to evaluate test coverage. In order to view test coverage, please use the --cov option:

gs_quant>pytest --cov=gs_quant --cov-report gs_quant/test/timeseries/

If you want to know which statements are missing tests, use the following option:

gs_quant>pytest --cov=gs_quant --cov-report term-missing gs_quant/test

Happy testing!

Related Content