Python coverage crashing when using unittest.mock - python

I have a Python library in which I am setting up some tests. As parts of the tests I am using MagicMock to mock some features like JSON load and files openning and closing. The tests work, everything works as expected.
The issue comes when I try to run the tests with coverage. Since I introduced the new tests that use mock I get coverage crashing.
This is the part where I am implementing the mocking:
def setUp(self):
# Setup the content of the config files for the tests
json.load = MagicMock(side_effect=file_content)
# Opening a file returns the name of the file
def get_mock_context(filename):
mock_context = MagicMock()
mock_context.__enter__.return_value = filename
mock_context.__exit__.return_value = False
return mock_context
builtins.open = MagicMock(side_effect=get_mock_context)
As mentioned, when I run the tests everything seems to be working fine
python -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.322s
OK
Nonetheless, as soon as I introduce coverage:
coverage run -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.759s
OK
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 782, in save
self.data_files.write(self.data, suffix=self.data_suffix)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 680, in write
data.write_file(filename)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 467, in write_file
with open(filename, 'w') as fdata:
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 951, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 1017, in _mock_call
result = effect(*args, **kwargs)
TypeError: get_mock_context() takes 1 positional argument but 2 were given
I have no idea what the issue might be. I have tried excluding the lines in the tests files that include mocking with pragma: no cover but that did nothing. I cannot find a way to make this work again. Any ideas or something you think I might have missed?
EDIT:
I just cleaned a bit the code y pasted before. If I run coverage specifying that I want to to use as source only the files in my local directory (to avoid using coverage in python libraries etc) I get the following error. Still not working, but a different one:
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 781, in save
self.get_data()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 834, in get_data
self._post_save_work()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 864, in _post_save_work
self._find_unexecuted_files(src)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 913, in _find_unexecuted_files
for file_path, plugin_name in itertools.chain(py_files, plugin_files):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 910, in <genexpr>
py_files = ((py_file, None) for py_file in find_python_files(src_dir))
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/files.py", line 416, in find_python_files
del dirnames[:]
TypeError: 'tuple' object does not support item deletion

Related

Python buildozer, ValueError

Good afternoon! I'm asking for help, I can't figure out python buildozer for a day, I've already prepared everything, and at the final stage, when the "buildozer android debug" command is entered into the terminal, it gives such an error at the end. Can anyone tell me what to do about it and how to fix it?
Here is the error itself:
Traceback (most recent call last):
File "/usr/local/bin/buildozer", line 11, in <module>
load_entry_point('buildozer==0.40.dev0', 'console_scripts', 'buildozer')()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/scripts/client.py", line 13, in main
Buildozer().run_command(sys.argv[1:])
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 1071, in run_command
self.target.run_commands(args)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/target.py", line 92, in run_commands
func(args)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/target.py", line 102, in cmd_debug
self.buildozer.prepare_for_build()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 178, in prepare_for_build
self.target.install_platform()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/targets/android.py", line 666, in install_platform
self._install_android_ndk()
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/targets/android.py", line 459, in _install_android_ndk
cwd=self.buildozer.global_platform_dir)
File "/usr/local/lib/python3.6/dist-packages/buildozer-0.40.dev0-py3.6.egg/buildozer/__init__.py", line 699, in download
urlretrieve(url, filename, report_hook)
File "/usr/lib/python3.6/urllib/request.py", line 1826, in retrieve
block = fp.read(bs)
File "/usr/lib/python3.6/tempfile.py", line 624, in func_wrapper
return func(*args, **kwargs)
ValueError: read of closed file
I tried to find an answer to this question, but so far without success.

AttributeError: module 'builtins' has no attribute 'interpreter'

It is a basic question, I guess.
I can run my program in Pycharm 2018.1 Python 3.6.5. But when I run in debug mode and open python console, I get this error message when I try to key in any python command.
I am using virtualenv and added interpreter in project setting.
I wonder what's wrong with my configuration
below is stacktrace
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevconsole.py", line 362, in get_interpreter
interpreterInterface = getattr(__builtin__, 'interpreter')
AttributeError: module 'builtins' has no attribute 'interpreter'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydevd_bundle/pydevd_comm.py", line 1443, in do_it
result = pydevconsole.console_exec(self.thread_id, self.frame_id, self.expression, dbg)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevconsole.py", line 467, in console_exec
need_more = exec_code(CodeFragment(expression), updated_globals, frame.f_locals, dbg)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevconsole.py", line 383, in exec_code
interpreterInterface = get_interpreter()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevconsole.py", line 364, in get_interpreter
interpreterInterface = InterpreterInterface(None, None, threading.currentThread())
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console.py", line 24, in __init__
self.interpreter = get_pydev_frontend(host, client_port)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 461, in get_pydev_frontend
_PyDevFrontEndContainer._instance = _PyDevFrontEnd()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 310, in __init__
self.ipython = PyDevTerminalInteractiveShell.instance()
File "/Users/hobart/envs/pys/lib/python3.6/site-packages/traitlets/config/configurable.py", line 412, in instance
inst = cls(*args, **kwargs)
File "/Users/hobart/envs/pys/lib/python3.6/site-packages/IPython/terminal/interactiveshell.py", line 430, in __init__
super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
File "/Users/hobart/envs/pys/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 516, in __init__
self.init_completer()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 234, in init_completer
self.Completer = self._new_completer_500()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 196, in _new_completer_500
parent=self
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 87, in __init__
self.matchers.remove(self.python_matches)
ValueError: list.remove(x): x not in list
This was fixed few days after your question: https://youtrack.jetbrains.com/issue/PY-29313
You should update your PyCharm to 2018.1.1 or higher.
I had exactly the same problem after trying to install some packages into the virtual environment. For me it was solved by recreating the environment.

Robot Frame work: Error While running .robot file in python interpreter using robot.run (windows environment)

I am trying to execute Run_Keyword.robot made with RIDE file which is in directory 'Robot' from python interpreter and getting following error. I am able to run this .py file from cmd without any error and it gives expected results. What can be the reason for this. I am a newbie to Robot Framework. Please help with this.
from robot import run
run('C:\\Users\\uvijayac\\Desktop\\Robot')
The Error I am getting is as follows.
Traceback (most recent call last):
File "C:\Users\uvijayac\Desktop\Robot\rf.py", line 27, in <module>
run_tests()
File "C:\Users\uvijayac\Desktop\Robot\rf.py", line 23, in run_tests
report=report_file)
File "C:\Python27\lib\site-packages\robot\run.py", line 471, in run
return RobotFramework().execute(*datasources, **options)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 83, in execute
return self._execute(list(arguments), options)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 89, in _execute
return self._report_error(unicode(err), help=True)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 110, in _report_error
self._logger.error(message)
File "C:\Python27\lib\site-packages\robot\output\loggerhelper.py", line 59, in error
self.write(msg, 'ERROR')
File "C:\Python27\lib\site-packages\robot\output\loggerhelper.py", line 62, in write
self.message(Message(message, level, html))
File "C:\Python27\lib\site-packages\robot\output\logger.py", line 109, in message
logger.message(msg)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 66, in message
self._writer.error(msg.message, msg.level, clear=self._running_test)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 142, in error
self._highlight('[ ', level, ' ] ' + message, error=True)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 158, in _highlight
self._write(before, newline=False, error=error)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 154, in _write
stream.flush()
IOError: [Errno 9] Bad file descriptor
>>>

'Context' object has no attribute 'stdout_capture'

I'm new to python and bdd and need some help, please. I'm trying to get junit reports from python behave, but after each test I got this error:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 271, in <module>
_BehaveRunner(my_config, base_dir).run()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\_bdd_utils.py", line 96, in run
self._run_tests()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 189, in _run_tests
self.__real_runner.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 672, in run
return self.run_with_paths()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 693, in run_with_paths
return self.run_model()
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\behave_runner.py", line 112, in run_model
return super(_RunnerWrapper, self).run_model(features)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 483, in run_model
failed = feature.run(self)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\model.py", line 523, in run
failed = scenario.run(runner)
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\model.py", line 914, in run
self.stdout = runner.context.stdout_capture.getvalue()
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\behave\runner.py", line 214, in __getattr__
raise AttributeError(msg)
AttributeError: 'Context' object has no attribute 'stdout_capture'
By the way flags in behave.ini are next:
[behave]
junit=True
stdout_capture=True
stderr_capture=True
I solve this. If you use ide to run test, as I do (PyCharm), then try to run it from command line and all should work fine.

Error using a YAML config file with Python nosetests

def __init__():
with open("config.yaml", 'r') as yamlfile:
cfg = yaml.load(yamlfile)
self.obj = TestMe(cfg.get("general", "user"), cfg.get("general", "passwd")
My YAML File looks like this
general:
user : admin
passwd : admin
I run the test like this
"nosetests test.py --exe --with-xunit --tc-file config.yaml"
I get the following error:
Traceback (most recent call last):
File "/usr/local/bin/nosetests", line 9, in <module>
load_entry_point('nose==1.3.3', 'console_scripts', 'nosetests')()
File "/usr/local/lib/python2.7/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/usr/local/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/local/lib/python2.7/site-packages/nose/core.py", line 145, in parseArgs
self.config.configure(argv, doc=self.usage())
File "/usr/local/lib/python2.7/site-packages/nose/config.py", line 346, in configure
self.plugins.configure(options, self)
File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 284, in configure
cfg(options, config)
File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 99, in __call__
return self.call(*arg, **kw)
File "/usr/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 167, in simple
result = meth(*arg, **kw)
File "/usr/local/lib/python2.7/site-packages/testconfig.py", line 129, in configure
options.testconfigencoding)
File "/usr/local/lib/python2.7/site-packages/testconfig.py", line 40, in load_ini
tmpconfig.readfp(f)
File "/usr/local/lib/python2.7/ConfigParser.py", line 324, in readfp
self._read(fp, filename)
File "/usr/local/lib/python2.7/ConfigParser.py", line 512, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: test_config.yaml, line: 1
u'general:\n'
I am confused because on Python CLI i can read the file without any issues.. ?!
The docs suggest that nose-testconfig assumes a INI file on default. You'll need to explicitly tell nose that it's a YAML file:
nosetests test.py --exe --with-xunit --tc-file config.yaml --tc-format yaml

Categories

Resources