diff --git a/.gitignore b/.gitignore index a31403a..d0cd9cd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,11 @@ *.py[cod] build/ dist/ +\.coverage +labkey\.egg-info/ +\.eggs/ # Project Artifacts .idea/ -*.iml \ No newline at end of file +*.iml +*.swp diff --git a/README.md b/README.md index 0aeb62d..ec4a5af 100644 --- a/README.md +++ b/README.md @@ -106,16 +106,21 @@ LabKey Server v15.1 and later. This package is maintained by [LabKey](http://www.labkey.com/). If you have any questions or need support, please use the [LabKey Server developer support forum](https://www.labkey.org/home/developer/forum/project-start.view). ### Testing -If you are looking to contribute please run the tests before issuing a PR. For now you need to manually get the dependencies: +If you are looking to contribute please run the tests before issuing a PR. The tests can be initiated by running ```bash -$ pip install mock +$ python setup.py test ``` -Then, to run the tests: +This runs the tests using [pytest](https://docs.pytest.org/en/latest/contents.html). If you'd like to run pytest directly you can install the testing dependencies in your virtual environment with: ```bash -$ python test/test_labkey.py +$ pip install -e .[test] +``` + +Then, the tests can be run with +```bash +$ pytest . ``` ### Maintainers diff --git a/setup.cfg b/setup.cfg index 224a779..b428f47 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,12 @@ [metadata] -description-file = README.md \ No newline at end of file +description-file = README.md + +[aliases] +test=pytest + +[tool:pytest] +addopts = -v --cov=labkey --cov-config=setup.cfg --cov-report=html --cov-report=term + +[coverage:html] +directory = build/coverage_html +title = Test coverage report for labkey diff --git a/setup.py b/setup.py index 1888713..902ddb8 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,8 @@ long_desc = "Python client API for LabKey Server. Supports query and experiment APIs." +tests_require = ['pytest', 'requests', 'mock', 'pytest-cov'] + setup( name='labkey', version=version, @@ -50,7 +52,11 @@ packages=packages, package_data={}, install_requires=['requests'], - tests_require=['requests', 'mock'], + tests_require=tests_require, + setup_requires=['pytest-runner'], + extras_require={ + 'test': tests_require + }, keywords="labkey api client", classifiers=[ 'Development Status :: 4 - Beta', diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 0000000..9f81162 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,6 @@ +from labkey import utils + +# The mocks in the tests expect a request only to be made to the appropriate action. +# This flag disables the CSRF check built into ServerContext.make_request() so the tests +# get consistent results. +utils.DISABLE_CSRF_CHECK = True diff --git a/test/test_domain.py b/test/test_domain.py index 2e518ca..61ff2c4 100644 --- a/test/test_domain.py +++ b/test/test_domain.py @@ -28,7 +28,7 @@ from labkey.domain import create, Domain, drop, get, infer_fields, save from labkey.exceptions import RequestAuthorizationError -from utilities import MockLabKey, mock_server_context, success_test, success_test_get, throws_error_test, throws_error_test_get +from .utilities import MockLabKey, mock_server_context, success_test, success_test_get, throws_error_test, throws_error_test_get domain_controller = 'property' diff --git a/test/test_experiment_api.py b/test/test_experiment_api.py index f321b58..5fe75ec 100644 --- a/test/test_experiment_api.py +++ b/test/test_experiment_api.py @@ -25,7 +25,7 @@ from labkey.experiment import load_batch, save_batch, Batch, Run from labkey.exceptions import RequestError, QueryNotFoundError, ServerNotFoundError, RequestAuthorizationError -from utilities import MockLabKey, mock_server_context, success_test, throws_error_test +from .utilities import MockLabKey, mock_server_context, success_test, throws_error_test class MockLoadBatch(MockLabKey): diff --git a/test/test_labkey.py b/test/test_labkey.py deleted file mode 100644 index de40bfd..0000000 --- a/test/test_labkey.py +++ /dev/null @@ -1,42 +0,0 @@ -# -# Copyright (c) 2016-2018 LabKey Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -from __future__ import unicode_literals -import unittest - -from labkey import utils -from test_domain import suite as domain_suite -from test_experiment_api import suite as exp_suite -from test_query_api import suite as query_suite -from test_security import suite as security_suite -from test_unsupported import suite as unsupported_suite -from test_utils import suite as utils_suite - -if __name__ == '__main__': - - # The mocks in the tests expect a request only to be made to the appropriate action. - # This flag disables the CSRF check built into ServerContext.make_request() so the tests - # get consistent results. - utils.DISABLE_CSRF_CHECK = True - - all_tests = unittest.TestSuite([ - domain_suite(), - exp_suite(), - query_suite(), - security_suite(), - unsupported_suite(), - utils_suite() - ]) - unittest.TextTestRunner().run(all_tests) diff --git a/test/test_query_api.py b/test/test_query_api.py index 909da33..340ea6d 100644 --- a/test/test_query_api.py +++ b/test/test_query_api.py @@ -25,7 +25,7 @@ from labkey.query import delete_rows, update_rows, insert_rows, select_rows, execute_sql, QueryFilter from labkey.exceptions import RequestError, QueryNotFoundError, ServerNotFoundError, RequestAuthorizationError -from utilities import MockLabKey, mock_server_context, success_test, throws_error_test +from .utilities import MockLabKey, mock_server_context, success_test, throws_error_test class MockSelectRows(MockLabKey): diff --git a/test/test_security.py b/test/test_security.py index 0852f1d..8267066 100644 --- a/test/test_security.py +++ b/test/test_security.py @@ -27,7 +27,7 @@ remove_from_group, remove_from_role, add_to_role, get_roles, list_groups from labkey.exceptions import RequestError, QueryNotFoundError, ServerNotFoundError, RequestAuthorizationError -from utilities import MockLabKey, mock_server_context, success_test, throws_error_test +from .utilities import MockLabKey, mock_server_context, success_test, throws_error_test class MockSecurityController(MockLabKey): diff --git a/test/test_unsupported.py b/test/test_unsupported.py index c17fec5..1c7a8a7 100644 --- a/test/test_unsupported.py +++ b/test/test_unsupported.py @@ -19,7 +19,7 @@ from labkey import utils from labkey.unsupported import messageboard -from utilities import MockLabKey, mock_server_context, success_test +from .utilities import MockLabKey, mock_server_context, success_test class MockPostMessage(MockLabKey):