Search code examples
pythonjirapytestpython-jira

How to avoid hard coded username/password in jira.cfg for pytest_jira plugin


I am trying to integrate pytest-jira plugin to my python script. I am referring to this link.

It is not feasible to hard code username/password in clear text form in ~/jira.cfg. Is there any way I can override them from outside? Basically I am looking for ways to skip hard coding of password in jira.cfg.

username = USERNAME (or blank for no authentication
password = PASSWORD (or blank for no authentication)

Can someone suggest a way to do this?


Solution

  • You can simply overwrite the arguments by defining your own pytest_configure hook. That's the beauty of pytest: you can redefine almost everything and adapt it to your particular needs. Example with reading username/password from environment variables:

    # conftest.py
    
    import os
    import pytest
    
    
    def pytest_configure(config):
        username = os.getenv('JIRA_USER', None)
        password = os.getenv('JIRA_PASSWORD', None)
        if username and password:
            config.option.jira_username = username
            config.option.jira_password = password
    

    That's it! Test it:

    $ export JIRA_USER=me JIRA_PASSWORD=mypass
    $ pytest --jira