We are using some tools for testing modifications and new features additions.
- automated tests using travis and pytest.
- coverage.py, a tool for measuring code coverage of Python programs.
Next we'll explain the steps to test your code. When a test is run, a .
means
that the test passed and an F
means it failed.
-
In your environment, install
pytest
by runningpip install pytest
orconda install -c conda-forge pytest
-
Now, simply run
pytest
, and it will give you and output like this if all tests passed
++ pytest --showlocals --pyargs pymove/
============================================== test session starts ==============================================
platform linux -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /home/flycher/Documentos/PyMove, inifile: setup.cfg
collected 95 items
pymove/tests/test_init.py . [ 1%]
pymove/tests/core/test_data_frame.py ......................................................................[ 74%]
pymove/tests/preprocessing/test_compression.py . [ 75%]
pymove/tests/preprocessing/test_filters.py ............... [ 91%]
pymove/tests/preprocessing/test_segmentation.py ..... [ 96%]
pymove/tests/preprocessing/test_stay_point_detection.py ... [100%]
====================================== 95 passed, 1 warning in 3.62s =============================================
- And like this if any tests fail
++ pytest --showlocals --pyargs pymove/
============================================== test session starts ==============================================
platform linux -- Python 3.7.6, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /home/flycher/Documentos/PyMove, inifile: setup.cfg
collected 95 items
pymove/tests/test_init.py . [ 1%]
pymove/tests/core/test_data_frame.py ......................................................................[ 74%]
pymove/tests/preprocessing/test_compression.py . [ 75%]
pymove/tests/preprocessing/test_filters.py .F............. [ 91%]
pymove/tests/preprocessing/test_segmentation.py ..... [ 96%]
pymove/tests/preprocessing/test_stay_point_detection.py ... [100%]
=================================================================================================================
def test_by_datetime():
move_df, cols = _prepare_df_default(list_data_1)
filter_values_start = filters.by_datetime(
move_df, start_datetime='2008-10-23 05:53:11')
expected = DataFrame(data=[
[39.984199952392578, 116.31932067871094, Timestamp('2008-10-23 05:53:06'), 1],
[39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2],
[39.984222412109375, 116.31940460205078, Timestamp('2008-10-23 05:53:11'), 2]
], columns=cols, index=[1, 2, 3]
)
> assert_frame_equal(filter_values_start, expected)
E AssertionError: DataFrame are different
E
E DataFrame shape mismatch
E [left]: (2, 4)
E [right]: (3, 4)
==================================== short test summary info =====================================================
FAILED pymove/tests/preprocessing/test_filters.py::test_by_datetime - AssertionError: DataFrame are different
================================= 1 failed, 94 passed, 1 warning in 3.68s ========================================
If a new the code was modified, it is essential to run against the existing tests and implement new tests if new features were created. To create a new test, you can follow the structure similar to the source code, for example
-
If creating a new function named
filter_by_city
in the modulepymove/preprocessing/filter.py
, the test must be implemented in the filepymove/tests/test_preprocessing_filters.py
, namedtest_filter_by_city
-
If creating a new module, for example
pymove/models/clustering/hierarchical.py
, the tests must be placed inpymove/tests/test_models_clustering_hierarchical.py
-
Every test must be independent from another, and if using files, you must use the
tmpdir
directive, like bellow. More on https://code-maven.com/temporary-files-and-directory-for-pytest
def test_some_thing(tmpdir):
d = tmpdir.mkdir('folder')
f = d.join('name.csv')
f.write(data)
filename = os.path.join(
f.dirname, f.basename
)
with open(filename, 'r') as f:
...
Coverage.py
allows us to se which lines of code are beign covered bt our tests
To use it, install running pip install coverage
or conda install -c conda-forge coverage
-
To create the report, run
coverage run -m pytest
-
To view the report, run
coverage report
-
The
coverage html
will create an html page that you can navigate, for that, open the fiehtmlcov/index.html
-
To erase the data, run
coverage erase