├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST.in ├── README.rst ├── appveyor.yml ├── examples ├── example_cfg.ini ├── example_cfg.json ├── example_cfg.py ├── example_cfg.yaml ├── example_cfg2.ini ├── example_cfg2.py └── test_config.py ├── pytest_testconfig.py ├── setup.py ├── tests ├── conftest.py └── test_testconfig.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask instance folder 57 | instance/ 58 | 59 | # Sphinx documentation 60 | docs/_build/ 61 | 62 | # MkDocs documentation 63 | /site/ 64 | 65 | # PyBuilder 66 | target/ 67 | 68 | # IPython Notebook 69 | .ipynb_checkpoints 70 | 71 | # pyenv 72 | .python-version 73 | 74 | # pycharm cache 75 | .idea 76 | 77 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Config file for automatic testing at travis-ci.org 2 | 3 | sudo: false 4 | language: python 5 | 6 | matrix: 7 | include: 8 | - python: 2.7 9 | env: TOX_ENV=py27 10 | - python: 3.5 11 | env: TOX_ENV=py35 12 | - python: 3.6 13 | env: TOX_ENV=py36 14 | - python: 3.7 15 | env: TOX_ENV=py37 16 | - python: 3.8 17 | env: TOX_ENV=py38 18 | - python: pypy 19 | env: TOX_ENV=pypy 20 | 21 | install: 22 | - pip install tox 23 | 24 | script: 25 | - tox -e $TOX_ENV 26 | 27 | before_cache: 28 | - rm -rf $HOME/.cache/pip/log 29 | 30 | cache: 31 | directories: 32 | - $HOME/.cache/pip 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | 180 | Copyright 2018 Wojciech Olejarz, Bartłomiej Skrobek 181 | 182 | Licensed under the Apache License, Version 2.0 (the "License"); 183 | you may not use this file except in compliance with the License. 184 | You may obtain a copy of the License at 185 | 186 | http://www.apache.org/licenses/LICENSE-2.0 187 | 188 | Unless required by applicable law or agreed to in writing, software 189 | distributed under the License is distributed on an "AS IS" BASIS, 190 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 191 | See the License for the specific language governing permissions and 192 | limitations under the License. 193 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.rst 3 | recursive-include tests 4 | 5 | recursive-exclude * __pycache__ 6 | recursive-exclude * *.py[co] 7 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ================= 2 | pytest-testconfig 3 | ================= 4 | 5 | .. image:: https://img.shields.io/pypi/v/pytest-testconfig.svg 6 | :target: https://pypi.org/project/pytest-testconfig 7 | :alt: PyPI version 8 | 9 | .. image:: https://img.shields.io/pypi/pyversions/pytest-testconfig.svg 10 | :target: https://pypi.org/project/pytest-testconfig 11 | :alt: Python versions 12 | 13 | .. image:: https://travis-ci.org/wojole/pytest-testconfig.svg?branch=master 14 | :target: https://travis-ci.org/wojole/pytest-testconfig 15 | :alt: See Build Status on Travis CI 16 | 17 | 18 | Test configuration plugin for pytest. 19 | 20 | Based on nose-testconfig by Jesse Noller. Rewritten for pytest by Wojciech Olejarz and Bartłomiej Skrobek. 21 | 22 | ---- 23 | 24 | This `pytest`_ plugin was generated with `Cookiecutter`_ along with `@hackebrot`_'s `cookiecutter-pytest-plugin`_ template. 25 | 26 | 27 | Features 28 | -------- 29 | pytest-testconfig is a plugin to the pytest test framework used for passing test-specific (or test-run specific) configuration data 30 | to the tests being executed. 31 | 32 | Currently configuration files in the following formats should be supported: 33 | 34 | - YAML (via `PyYAML `_) 35 | - INI (via `ConfigParser `_) 36 | - Pure Python (via Exec) 37 | - JSON (via `JSON `_) 38 | 39 | The plugin is ``meant`` to be flexible, ergo the support of exec'ing arbitrary 40 | python files as configuration files with no checks. The default format is 41 | assumed to be ConfigParser ini-style format. 42 | 43 | If multiple files are provided, the objects are merged. Later settings will 44 | override earlier ones. 45 | 46 | The plugin provides a method of overriding certain parameters from the command 47 | line (assuming that the main "config" object is a dict) and can easily have 48 | additional parsers added to it. 49 | 50 | A configuration file may not be provided. In this case, the config object is an 51 | emtpy dict. Any command line "overriding" paramters will be added to the dict. 52 | 53 | 54 | Requirements 55 | ------------ 56 | 57 | requires pytest>=3.5.0 58 | 59 | 60 | Installation 61 | ------------ 62 | 63 | You can install "pytest-testconfig" via `pip`_ from `PyPI`_:: 64 | 65 | $ python3 -m pip install pytest-testconfig 66 | 67 | 68 | Usage 69 | ----- 70 | 71 | Tests can import the "config" singleton from testconfig:: 72 | 73 | from pytest_testconfig import config 74 | 75 | By default, YAML files parse into a nested dictionary, and ConfigParser ini 76 | files are also collapsed into a nested dictionary for foo[bar][baz] style 77 | access. Tests can obviously access configuration data by referencing the 78 | relevant dictionary keys:: 79 | 80 | from pytest_testconfig import config 81 | def test_foo(): 82 | target_server_ip = config['servers']['webapp_ip'] 83 | 84 | ``Warning``: Given this is just a dictionary singleton, tests can easily write 85 | into the configuration. This means that your tests can write into the config 86 | space and possibly alter it. This also means that threaded access into the 87 | configuration can be interesting. 88 | 89 | When using pure python configuration - obviously the "sky is the the limit" - 90 | given that the configuration is loaded via an exec, you could potentially 91 | modify pytest, the plugin, etc. However, if you do not export a config{} dict 92 | as part of your python code, you obviously won't be able to import the 93 | config object from testconfig. 94 | 95 | When using YAML-style configuration, you get a lot of the power of pure python 96 | without the danger of unprotected exec() - you can obviously use the pyaml 97 | python-specific objects and all of the other YAML creamy goodness. 98 | 99 | Defining a configuration file 100 | ----------------------------- 101 | 102 | Simple ConfigParser style:: 103 | 104 | [myapp_servers] 105 | main_server = 10.1.1.1 106 | secondary_server = 10.1.1.2 107 | 108 | So your tests access the config options like this:: 109 | 110 | from pytest_testconfig import config 111 | def test_foo(): 112 | main_server = config['myapp_servers']['main_server'] 113 | 114 | YAML style configuration:: 115 | myapp: 116 | servers: 117 | main_server: 10.1.1.1 118 | secondary_server: 10.1.1.2 119 | 120 | And your tests can access it thus:: 121 | 122 | from pytest_testconfig import config 123 | def test_foo(): 124 | main_server = config['myapp']['servers']['main_server'] 125 | 126 | Python configuration file:: 127 | 128 | import socket 129 | 130 | global config 131 | config = {} 132 | possible_main_servers = ['10.1.1.1', '10.1.1.2'] 133 | 134 | for srv in possible_main_servers: 135 | try: 136 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 137 | s.connect((srv, 80)) 138 | except: 139 | continue 140 | s.close() 141 | config['main_server'] = srv 142 | break 143 | 144 | And lo, the config is thus:: 145 | 146 | from pytest_testconfig import config 147 | def test_foo(): 148 | main_server = config['main_server'] 149 | 150 | If you need to put python code into your configuration, you either need to use 151 | the python-config file faculties, or you need to use the !!python tags within 152 | PyYAML/YAML - raw ini files no longer have any sort of eval magic. 153 | 154 | Command line options 155 | -------------------- 156 | 157 | After it is installed, the plugin adds the following command line flags to 158 | pytest:: 159 | 160 | --tc-file=TESTCONFIG Configuration file to parse and pass to tests 161 | [PY_TEST_CONFIG_FILE] 162 | If this is specified multiple times, all files 163 | will be parsed. In all formats except python, 164 | previous contents are preserved and the configs 165 | are merged. 166 | 167 | --tc-format=TESTCONFIGFORMAT Test config file format, default is 168 | configparser ini format 169 | [PY_TEST_CONFIG_FILE_FORMAT] 170 | 171 | --tc=OVERRIDES Option:Value specific overrides. 172 | 173 | --tc-exact Optional: Do not explode periods in override keys to 174 | individual keys within the config dict, instead treat 175 | them as config[my.toplevel.key] ala sqlalchemy.url in 176 | pylons. 177 | 178 | Contributing 179 | ------------ 180 | Contributions are very welcome. Tests can be run with `tox`_, please ensure 181 | the coverage at least stays the same before you submit a pull request. 182 | 183 | License 184 | ------- 185 | 186 | Distributed under the terms of the `Apache Software License 2.0`_ license, "pytest-testconfig" is free and open source software 187 | 188 | 189 | Issues 190 | ------ 191 | 192 | If you encounter any problems, please `file an issue`_ along with a detailed description. 193 | 194 | .. _`Cookiecutter`: https://github.com/audreyr/cookiecutter 195 | .. _`@hackebrot`: https://github.com/hackebrot 196 | .. _`MIT`: http://opensource.org/licenses/MIT 197 | .. _`BSD-3`: http://opensource.org/licenses/BSD-3-Clause 198 | .. _`GNU GPL v3.0`: http://www.gnu.org/licenses/gpl-3.0.txt 199 | .. _`Apache Software License 2.0`: http://www.apache.org/licenses/LICENSE-2.0 200 | .. _`cookiecutter-pytest-plugin`: https://github.com/pytest-dev/cookiecutter-pytest-plugin 201 | .. _`file an issue`: https://github.com/wojole/pytest-testconfig/issues 202 | .. _`pytest`: https://github.com/pytest-dev/pytest 203 | .. _`tox`: https://tox.readthedocs.io/en/latest/ 204 | .. _`pip`: https://pypi.org/project/pip/ 205 | .. _`PyPI`: https://pypi.org/project 206 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # What Python version is installed where: 2 | # http://www.appveyor.com/docs/installed-software#python 3 | 4 | environment: 5 | matrix: 6 | - PYTHON: "C:\\Python27" 7 | TOX_ENV: "py27" 8 | 9 | - PYTHON: "C:\\Python34" 10 | TOX_ENV: "py34" 11 | 12 | - PYTHON: "C:\\Python35" 13 | TOX_ENV: "py35" 14 | 15 | - PYTHON: "C:\\Python36" 16 | TOX_ENV: "py36" 17 | 18 | init: 19 | - "%PYTHON%/python -V" 20 | - "%PYTHON%/python -c \"import struct;print( 8 * struct.calcsize(\'P\'))\"" 21 | 22 | install: 23 | - "%PYTHON%/Scripts/easy_install -U pip" 24 | - "%PYTHON%/Scripts/pip install tox" 25 | - "%PYTHON%/Scripts/pip install wheel" 26 | 27 | build: false # Not a C# project, build stuff at the test step instead. 28 | 29 | test_script: 30 | - "%PYTHON%/Scripts/tox -e %TOX_ENV%" 31 | 32 | after_test: 33 | - "%PYTHON%/python setup.py bdist_wheel" 34 | - ps: "ls dist" 35 | 36 | artifacts: 37 | - path: dist\* 38 | 39 | #on_success: 40 | # - TODO: upload the content of dist/*.whl to a public wheelhouse 41 | -------------------------------------------------------------------------------- /examples/example_cfg.ini: -------------------------------------------------------------------------------- 1 | # INI 2 | [myapp_servers] 3 | main_server = 10.1.1.1 4 | secondary_server = 10.1.1.2 5 | -------------------------------------------------------------------------------- /examples/example_cfg.json: -------------------------------------------------------------------------------- 1 | { 2 | "myapp": { 3 | "servers": { 4 | "main_server": "10.1.1.1", 5 | "secondary_server": "10.1.1.2" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /examples/example_cfg.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | global config 4 | config = {} 5 | possible_main_servers = ['10.1.1.1', '10.1.1.2'] 6 | 7 | for srv in possible_main_servers: 8 | try: 9 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 10 | s.connect((srv, 80)) 11 | except: 12 | continue 13 | s.close() 14 | config['main_server'] = srv 15 | break 16 | -------------------------------------------------------------------------------- /examples/example_cfg.yaml: -------------------------------------------------------------------------------- 1 | # YAML, that spells moon. 2 | myapp: 3 | servers: 4 | main_server: 10.1.1.1 5 | secondary_server: 10.1.1.2 -------------------------------------------------------------------------------- /examples/example_cfg2.ini: -------------------------------------------------------------------------------- 1 | # INI 2 | [myapp_servers] 3 | secondary_server = 10.1.1.3 4 | -------------------------------------------------------------------------------- /examples/example_cfg2.py: -------------------------------------------------------------------------------- 1 | global config 2 | config = { 3 | 'myapp': { 4 | 'servers': { 5 | 'main_server': '.'.join(('10', '1', '1', '1',)), 6 | } 7 | } 8 | } 9 | 10 | -------------------------------------------------------------------------------- /examples/test_config.py: -------------------------------------------------------------------------------- 1 | # 2 | # from testconfig import config 3 | # import pprint 4 | # 5 | # def test_ohnoes(): 6 | # pprint.pprint(config) 7 | -------------------------------------------------------------------------------- /pytest_testconfig.py: -------------------------------------------------------------------------------- 1 | """Pytest-testconfig is a py.test plugin which provides passing test-specific 2 | (or test-run specific) configuration data to the tests being executed. 3 | Plugin is based on nose-testconfig plugin which provided same capabilities for 4 | nosetests framework 5 | """ 6 | 7 | import logging 8 | import os 9 | import re 10 | import codecs 11 | 12 | 13 | try: 14 | import ConfigParser 15 | except ImportError: 16 | import configparser as ConfigParser 17 | # Import, or define, NullHandler 18 | try: 19 | from logging import NullHandler 20 | except ImportError: 21 | from logging import Handler 22 | 23 | class NullHandler(Handler): 24 | 25 | """No-op handler.""" 26 | 27 | def emit(self, record): 28 | """Intentionally do nothing.""" 29 | pass 30 | 31 | log = logging.getLogger(__name__) 32 | log.addHandler(NullHandler()) 33 | 34 | 35 | warning = "Cannot access the test config because the plugin has not \ 36 | been activated. Did you specify --tc or any other command line option?" 37 | 38 | config = {} 39 | py_config = {} 40 | 41 | 42 | def tolist(val): 43 | """Convert a value that may be a list or a (possibly comma-separated) 44 | string into a list. The exception: None is returned as None, not [None]. 45 | """ 46 | if val is None: 47 | return None 48 | try: 49 | # might already be a list 50 | val.extend([]) 51 | return val 52 | except AttributeError: 53 | pass 54 | # might be a string 55 | try: 56 | return re.split(r'\s*,\s*', val) 57 | except TypeError: 58 | # who knows... 59 | return list(val) 60 | 61 | 62 | def merge_map(original, to_add): 63 | """ Merges a new map of configuration recursively with an older one """ 64 | for k, v in to_add.items(): 65 | if isinstance(v, dict) and k in original and isinstance(original[k], 66 | dict): 67 | merge_map(original[k], v) 68 | else: 69 | original[k] = v 70 | 71 | 72 | def load_yaml(yaml_file, encoding): 73 | """ Load the passed in yaml configuration file """ 74 | try: 75 | import yaml 76 | except (ImportError): 77 | raise Exception('unable to import YAML package. Can not continue.') 78 | global config 79 | with codecs.open(os.path.expanduser(yaml_file), 'r', encoding) as f: 80 | parsed_config = yaml.safe_load(f.read()) 81 | merge_map(config, parsed_config) 82 | 83 | 84 | def load_ini(ini_file, encoding): 85 | """ Parse and collapse a ConfigParser-Style ini file into a two-level 86 | config structure. """ 87 | 88 | global config 89 | tmpconfig = ConfigParser.ConfigParser() 90 | # Overriding optionxform method to avoid lowercase conversion 91 | tmpconfig.optionxform = lambda override: override 92 | with codecs.open(os.path.expanduser(ini_file), 'r', encoding) as f: 93 | try: 94 | tmpconfig.read_file(f) 95 | except AttributeError: 96 | tmpconfig.readfp(f) 97 | 98 | parsed_config = {} 99 | for section in tmpconfig.sections(): 100 | parsed_config[section] = {} 101 | for option in tmpconfig.options(section): 102 | parsed_config[section][option] = tmpconfig.get(section, option) 103 | merge_map(config, parsed_config) 104 | 105 | 106 | def load_python(py_file, encoding): 107 | """ This will exec the defined python file into the config variable - 108 | the implicit assumption is that the python is safe, well formed and will 109 | not do anything bad. This is also dangerous. """ 110 | with codecs.open(os.path.expanduser(py_file), 'r', encoding) as f: 111 | exec(f.read()) 112 | 113 | 114 | def load_json(json_file, encoding): 115 | """ This will use the json module to to read in the config json file. 116 | """ 117 | import json 118 | global config 119 | with codecs.open(os.path.expanduser(json_file), 'r', encoding) as f: 120 | parsed_config = json.load(f) 121 | merge_map(config, parsed_config) 122 | 123 | 124 | enabled_option = False 125 | name = "test_config" 126 | score = 1 127 | 128 | env_opt = "PY_TEST_CONFIG_FILE" 129 | format_option = "ini" 130 | encoding_option = 'utf-8' 131 | valid_loaders = {'yaml': load_yaml, 'ini': load_ini, 'python': load_python, 'json': load_json} 132 | 133 | 134 | def pytest_addoption(parser, env=os.environ): 135 | """ Define the command line options for the plugin. """ 136 | group = parser.getgroup('test-config') 137 | group.addoption('--tc-file', 138 | action='append', 139 | dest='testconfig', 140 | default=[env.get(env_opt)], 141 | help="Configuration file to parse and pass to tests" 142 | " [PY_TEST_CONFIG_FILE]") 143 | group.addoption('--tc-file-encoding', 144 | action='store', 145 | dest='testconfigencoding', 146 | default=env.get('PY_TEST_CONFIG_FILE_ENCODING') or encoding_option, 147 | help="Test config file encoding, default is utf-8" 148 | " [PY_TEST_CONFIG_FILE_ENCODING]") 149 | group.addoption('--tc-format', 150 | action='store', 151 | dest='testconfigformat', 152 | default=env.get('PY_TEST_CONFIG_FILE_FORMAT') or format_option, 153 | help="Test config file format, default is configparser ini format" 154 | " [PY_TEST_CONFIG_FILE_FORMAT]") 155 | group.addoption('--tc', 156 | action='append', 157 | dest='overrides', 158 | default=[], 159 | help="Option:Value specific overrides.") 160 | group.addoption('--tc-exact', 161 | action='store_true', 162 | dest='exact', 163 | default=False, 164 | help="Optional: Do not explode periods in override keys to " 165 | "individual keys within the config dict, instead treat them" 166 | " as config[my.toplevel.key] ala sqlalchemy.url in pylons") 167 | 168 | # Add github marker to --help 169 | parser.addini("github", "GitHub issue integration", "args") 170 | 171 | 172 | def pytest_cmdline_main(config): 173 | """ Call the super and then validate and call the relevant parser for 174 | the configuration file passed in """ 175 | global py_config 176 | if not config.getoption('testconfig') and not config.getoption('overrides'): 177 | return 178 | 179 | if config.getoption('testconfigformat'): 180 | format_option = config.getoption('testconfigformat') 181 | if format_option not in valid_loaders.keys(): 182 | raise Exception('%s is not a valid configuration file format' \ 183 | % format_option) 184 | 185 | # Load the configuration file: 186 | for configfile in config.getoption('testconfig'): 187 | if configfile: 188 | valid_loaders[format_option](configfile, 189 | config.getoption('testconfigencoding')) 190 | 191 | overrides = tolist(config.getoption('overrides')) or [] 192 | for override in overrides: 193 | keys, val = override.split(":", 1) 194 | if config.getoption('exact'): 195 | py_config[keys] = val 196 | else: 197 | # Create all *parent* keys that may not exist in the config 198 | section = py_config 199 | keys = keys.split('.') 200 | for key in keys[:-1]: 201 | if key not in section: 202 | section[key] = {} 203 | section = section[key] 204 | 205 | # Finally assign the value to the last key 206 | key = keys[-1] 207 | section[key] = val 208 | 209 | 210 | config = py_config 211 | 212 | # Use an environment hack to allow people to set a config file to auto-load 213 | # in case they want to put tests they write through pychecker or any other 214 | # syntax thing which does an execute on the file. 215 | if 'PYTEST_TESTCONFIG_AUTOLOAD_YAML' in os.environ: 216 | load_yaml(os.environ['PYTEST_TESTCONFIG_AUTOLOAD_YAML'], encoding='utf-8') 217 | 218 | if 'PYTEST_TESTCONFIG_AUTOLOAD_INI' in os.environ: 219 | load_ini(os.environ['PYTEST_TESTCONFIG_AUTOLOAD_INI'], encoding='utf-8') 220 | 221 | if 'PYTEST_TESTCONFIG_AUTOLOAD_PYTHON' in os.environ: 222 | load_python(os.environ['PYTEST_TESTCONFIG_AUTOLOAD_PYTHON'], encoding='utf-8') 223 | 224 | if 'PYTEST_TESTCONFIG_AUTOLOAD_JSON' in os.environ: 225 | load_json(os.environ['PYTEST_TESTCONFIG_AUTOLOAD_JSON'], encoding='utf-8') 226 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import codecs 6 | from setuptools import setup 7 | 8 | 9 | def read(fname): 10 | file_path = os.path.join(os.path.dirname(__file__), fname) 11 | return codecs.open(file_path, encoding='utf-8').read() 12 | 13 | 14 | setup( 15 | name='pytest-testconfig', 16 | version='0.2.0', 17 | author='Wojciech Olejarz, Bartlomiej Skrobek', 18 | author_email='olejarz.wojciech@gmail.com', 19 | maintainer='Wojciech Olejarz, Bartlomiej Skrobek', 20 | maintainer_email='olejarz.wojciech@gmail.com', 21 | license='Apache Software License 2.0', 22 | url='https://github.com/wojole/pytest-testconfig', 23 | description='Test configuration plugin for pytest.', 24 | long_description=read('README.rst'), 25 | py_modules=['pytest_testconfig'], 26 | python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*', 27 | install_requires=['pytest>=3.5.0', 'pyyaml'], 28 | classifiers=[ 29 | 'Development Status :: 4 - Beta', 30 | 'Framework :: Pytest', 31 | 'Intended Audience :: Developers', 32 | 'Topic :: Software Development :: Testing', 33 | 'Programming Language :: Python', 34 | 'Programming Language :: Python :: 2.7', 35 | 'Programming Language :: Python :: 3', 36 | 'Programming Language :: Python :: 3.5', 37 | 'Programming Language :: Python :: 3.6', 38 | 'Programming Language :: Python :: 3.7', 39 | 'Programming Language :: Python :: 3.8', 40 | 'Programming Language :: Python :: Implementation :: PyPy', 41 | 'Operating System :: OS Independent', 42 | 'License :: OSI Approved :: Apache Software License', 43 | ], 44 | entry_points={ 45 | 'pytest11': [ 46 | 'testconfig = pytest_testconfig', 47 | ], 48 | }, 49 | ) 50 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | pytest_plugins = 'pytester' 2 | -------------------------------------------------------------------------------- /tests/test_testconfig.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import os 3 | 4 | 5 | def test_tc_file_from_ini(testdir): 6 | """Test for parsing configuration from ini file.""" 7 | 8 | testdir_path = os.getcwd() 9 | 10 | # create a temporary ini configuration file 11 | testdir.makefile(".ini", example_cfg=""" 12 | # INI 13 | [myapp_servers1] 14 | main_server = 10.1.1.1 15 | secondary_server = 10.1.1.5 16 | """) 17 | 18 | testdir.makepyfile( 19 | """ 20 | from pytest_testconfig import config 21 | 22 | def test_foo(): 23 | 24 | target_server_ip = config['myapp_servers1']['main_server'] 25 | assert target_server_ip == '10.1.1.1' 26 | """ 27 | ) 28 | 29 | # run pytest with the following cmd args 30 | result = testdir.runpytest( 31 | '--tc-file={}/example_cfg.ini'.format(testdir_path), 32 | '-v' 33 | ) 34 | 35 | # fnmatch_lines does an assertion internally 36 | result.stdout.fnmatch_lines([ 37 | '*test_tc_file_from_ini.py::test_foo PASSED*', 38 | ]) 39 | 40 | # make sure that that we get a '0' exit code for the testsuite 41 | assert result.ret == 0 42 | 43 | 44 | def test_tc_file_from_json(testdir): 45 | """Test for parsing configuration from json file.""" 46 | 47 | testdir_path = os.getcwd() 48 | # create a temporary json configuration file 49 | testdir.makefile(".json", example_cfg="""{ 50 | "myapp2": { 51 | "servers": { 52 | "main_server": "10.1.1.1", 53 | "secondary_server": "10.1.1.1" 54 | } 55 | } 56 | } 57 | """) 58 | 59 | testdir.makepyfile( 60 | """ 61 | from pytest_testconfig import config 62 | 63 | def test_foo(): 64 | 65 | target_server_ip = config['myapp2']['servers']['main_server'] 66 | assert target_server_ip == '10.1.1.1' 67 | """ 68 | ) 69 | 70 | # run pytest with the following cmd args 71 | result = testdir.runpytest( 72 | '--tc-file={}/example_cfg.json'.format(testdir_path), 73 | '--tc-format=json', 74 | '-v' 75 | ) 76 | 77 | # fnmatch_lines does an assertion internally 78 | result.stdout.fnmatch_lines([ 79 | '*test_tc_file_from_json.py::test_foo PASSED*', 80 | ]) 81 | 82 | # make sure that that we get a '0' exit code for the testsuite 83 | assert result.ret == 0 84 | 85 | 86 | def test_tc_file_from_yaml(testdir): 87 | """Test for parsing configuration from yaml file.""" 88 | 89 | testdir_path = os.getcwd() 90 | # create a temporary yaml configuration file 91 | testdir.makefile(".yaml", example_cfg=""" 92 | # YAML 93 | myapp3: 94 | servers: 95 | main_server: 10.1.1.1 96 | secondary_server: 10.1.1.2 97 | """) 98 | 99 | testdir.makepyfile( 100 | """ 101 | from pytest_testconfig import config 102 | 103 | def test_foo(): 104 | 105 | target_server_ip = config['myapp3']['servers']['main_server'] 106 | assert target_server_ip == '10.1.1.1' 107 | """ 108 | ) 109 | 110 | # run pytest with the following cmd args 111 | result = testdir.runpytest( 112 | '--tc-file={}/example_cfg.yaml'.format(testdir_path), 113 | '--tc-format=yaml', 114 | '-v' 115 | ) 116 | 117 | # fnmatch_lines does an assertion internally 118 | result.stdout.fnmatch_lines([ 119 | '*test_tc_file_from_yaml.py::test_foo PASSED*', 120 | ]) 121 | 122 | # make sure that that we get a '0' exit code for the testsuite 123 | assert result.ret == 0 124 | 125 | 126 | def test_tc_override(testdir): 127 | """Test for overriding configuration.""" 128 | 129 | testdir.makepyfile( 130 | """ 131 | from pytest_testconfig import config 132 | 133 | def test_foo(): 134 | target_server_ip = config['myapp_servers4']['secondary_server'] 135 | assert target_server_ip == '10.1.1.1' 136 | """ 137 | ) 138 | 139 | # run pytest with the following cmd args 140 | result = testdir.runpytest( 141 | '--tc=myapp_servers4.secondary_server:10.1.1.1', 142 | '-v', 143 | '-s' 144 | ) 145 | 146 | # fnmatch_lines does an assertion internally 147 | result.stdout.fnmatch_lines([ 148 | '*test_tc_override.py::test_foo PASSED*', 149 | ]) 150 | 151 | # make sure that that we get a '0' exit code for the testsuite 152 | assert result.ret == 0 153 | 154 | 155 | def test_tc_exact_override(testdir): 156 | """Test for overriding configuration with exact option.""" 157 | 158 | testdir.makepyfile( 159 | """ 160 | from pytest_testconfig import config 161 | 162 | def test_foo(): 163 | target_server_ip = config['myapp_servers.secondary_server'] 164 | assert target_server_ip == '10.1.1.1' 165 | """ 166 | ) 167 | 168 | # run pytest with the following cmd args 169 | result = testdir.runpytest( 170 | '--tc=myapp_servers.secondary_server:10.1.1.1', 171 | '--tc-exact', 172 | '-v' 173 | ) 174 | 175 | # fnmatch_lines does an assertion internally 176 | result.stdout.fnmatch_lines([ 177 | '*test_tc_exact_override.py::test_foo PASSED*', 178 | ]) 179 | 180 | # make sure that that we get a '0' exit code for the testsuite 181 | assert result.ret == 0 182 | 183 | 184 | def test_tc_file_from_python(testdir): 185 | """Test for parsing configuration from python file.""" 186 | 187 | testdir_path = os.getcwd() 188 | # create a temporary py configuration file 189 | testdir.makefile(".py", example_cfg=""" 190 | global config 191 | config = { 192 | 'myapp5': { 193 | 'servers': { 194 | 'main_server': '.'.join(('10', '1', '1', '1',)), 195 | } 196 | } 197 | } 198 | """) 199 | 200 | testdir.makepyfile( 201 | """ 202 | from pytest_testconfig import config 203 | 204 | def test_foo(): 205 | 206 | target_server_ip = config['myapp5']['servers']['main_server'] 207 | assert target_server_ip == '10.1.1.1' 208 | """ 209 | ) 210 | 211 | # run pytest with the following cmd args 212 | result = testdir.runpytest( 213 | '--tc-file={}/example_cfg.py'.format(testdir_path), 214 | '--tc-format=python', 215 | '-v' 216 | ) 217 | 218 | # fnmatch_lines does an assertion internally 219 | result.stdout.fnmatch_lines([ 220 | '*test_tc_file_from_python.py::test_foo PASSED*', 221 | ]) 222 | 223 | # make sure that that we get a '0' exit code for the testsuite 224 | assert result.ret == 0 225 | 226 | 227 | def test_help_message(testdir): 228 | result = testdir.runpytest( 229 | '--help', 230 | ) 231 | # fnmatch_lines does an assertion internally 232 | result.stdout.fnmatch_lines([ 233 | 'test-config:', 234 | ' --tc-file=TESTCONFIG Configuration file to parse and pass to tests', 235 | ' [PY_TEST_CONFIG_FILE]', 236 | ' --tc-file-encoding=TESTCONFIGENCODING', 237 | ' Test config file encoding, default is utf-8', 238 | ' [PY_TEST_CONFIG_FILE_ENCODING]', 239 | ' --tc-format=TESTCONFIGFORMAT', 240 | ' Test config file format, default is configparser ini', 241 | ' format [PY_TEST_CONFIG_FILE_FORMAT]', 242 | ' --tc=OVERRIDES Option:Value specific overrides.', 243 | ' --tc-exact Optional: Do not explode periods in override keys to', 244 | ' individual keys within the config dict, instead treat', 245 | ' them as config[my.toplevel.key] ala sqlalchemy.url in', 246 | ' pylons', 247 | ]) 248 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # For more information about tox, see https://tox.readthedocs.io/en/latest/ 2 | [tox] 3 | envlist = py27,py35,py36,py37,py38,pypy 4 | 5 | [testenv] 6 | deps = pytest>=3.0 7 | commands = pytest {posargs:tests} 8 | 9 | [testenv:flake8] 10 | skip_install = true 11 | deps = flake8 12 | commands = flake8 pytest_testconfig.py setup.py tests 13 | --------------------------------------------------------------------------------