Home > database >  Handling password prompt in Python Click module Unittesting
Handling password prompt in Python Click module Unittesting

Time:07-22

I am new to python click and unittesting, I am wondering if someone can help me understanding how to handle the password prompt in unittest ?

I created a python module for restconf apis and here is I call the script

$ python restconf_cli.py GET -u developer -n sandbox-iosxe-latest-1.cisco.com -p Cisco-IOS-XE-native:native/version
Password: 
{
  "Cisco-IOS-XE-native:version": "17.3"
}

Now, I am writing the unittest to run the same command, but it stuck get and nothing happens, it may be waiting on password prompt but not sure, please advise

$ python -m unittest tests/restconf_cli_restconf_get_test.py 

Here is unittest file contents

#!/usr/bin/env python

from click.testing import CliRunner
import unittest
import restconf_cli

class RestconfCliRestconfGetTests(unittest.TestCase):
    # breakpoint()
    def test_restconf_get(self):
        runner = CliRunner()
        result = runner.invoke(restconf_cli.restconf_get, 
                               ['GET', 
                                '-u developer', 
                                '-n sandbox-iosxe-latest-1.cisco.com',
                                '-p Cisco-IOS-XE-native:native/version'
                                ])
        # print(result)
        self.assertEqual(result.exit_code, 0)

if __name__ == '__main__':
    unittest.main()

I tried with the breakpoint but still no output can be seen.

Thanks

CodePudding user response:

Here is the answer, I need to provide the input=my_secret

class RestconfCliRestconfGetTests(unittest.TestCase):
    def test_restconf_get(self):
        runner = CliRunner()
        result = runner.invoke(restconf_cli, 
                               ['GET', 
                                '--username', 'developer', 
                                '--hostname', 'sandbox-iosxe-latest-1.cisco.com',
                                '--path', 'Cisco-IOS-XE-native:native/version',
                                ], input='my_secret\n')
  • Related