I have simple tests in site_tests.py file:
import unittest
class SiteTests(unittest.TestCase):
def test(self):
self.assertEqual('a', 'b')
if __name__ == '__main__':
unittest.main()
When I run "Unittest in test_site.py" with default PyCharm configuration I'm getting:
Testing started at 23:45 ...
C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\_jb_unittest_runner.py" --path C:/testSiteDemoTests/site_tests.py
Launching unittests with arguments python -m unittest C:/testSiteDemoTests/site_tests.py in C:\testSiteDemoTests
b != a
Expected :a
Actual :b
<Click to see difference>
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals
old(self, first, second, msg)
File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 829, in assertEqual
assertion_func(first, second, msg=msg)
File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 1202, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 670, in fail
raise self.failureException(msg)
AssertionError: 'a' != 'b'
- a
+ b
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run
testMethod()
File "C:\testSiteDemoTests\site_tests.py", line 7, in test
self.assertEqual('a', 'b')
Ran 1 test in 0.000s
FAILED (failures=1)
Process finished with exit code 1
The last part is very interesting since running this file without _jb_unittest_runner.py so C:\testSite>python lost_hat_tests.py
the output is ok:
F
======================================================================
FAIL: test (__main__.SiteTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "lost_hat_tests.py", line 7, in test
self.assertEqual('a', 'b')
AssertionError: 'a' != 'b'
- a
+ b
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
Is there simple answer why that second message appearing in PyCharm runner?
I also run the test in console with _jb_unittest_runner.py - hmm two testSuites - interesting
C:\testSiteDemoTests>python "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\_jb_unittest_runner.py" --path C:/testSiteDemoTests/site_tests.py
##teamcity[enteredTheMatrix timestamp='2018-05-20T23:59:59.931']
Launching unittests with arguments python -m unittest C:/testSiteDemoTests/site_tests.py in C:\testSiteDemoTests
##teamcity[testCount timestamp='2018-05-20T23:59:59.946' count='1']
##teamcity[testSuiteStarted timestamp='2018-05-20T23:59:59.946' locationHint='python<C:\testSiteDemoTests>://site_tests' name='site_tests' nodeId='1' parentNodeId='0']
##teamcity[testSuiteStarted timestamp='2018-05-20T23:59:59.946' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests' name='SiteTests' nodeId='2' parentNodeId='1']
##teamcity[testStarted timestamp='2018-05-20T23:59:59.962' captureStandardOutput='true' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' name='test' nodeId='3' parentNodeId='2']
##teamcity[testFailed timestamp='2018-05-20T23:59:59.977' actual='b' details='Traceback (most recent call last):|n File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.3\helpers\pycharm\teamcity\diff_tools.py", line 32, in _patched_equals|n old(
self, first, second, msg)|n File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 829, in assertEqual|n assertion_func(first, second, msg=msg)|n File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\
lib\unittest\case.py", line 1202, in assertMultiLineEqual|n self.fail(self._formatMessage(msg, standardMsg))|n File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 670, in fail|n raise self.failureException(msg)
|nAssertionError: |'a|' != |'b|'|n- a|n+ b|n|n|nDuring handling of the above exception, another exception occurred:|n|nTraceback (most recent call last):|n File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in t
estPartExecutor|n yield|n File "C:\Users\testSite\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 605, in run|n testMethod()|n File "C:\testSiteDemoTests\site_tests.py", line 7, in test|n self.assertEqual(|'a|', |'b|')|n' e
xpected='a' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' message='|nb != a|n' name='test' nodeId='3' parentNodeId='2' type='comparisonFailure']
##teamcity[testFinished timestamp='2018-05-20T23:59:59.977' duration='30' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests.test' name='test' nodeId='3' parentNodeId='2']
Ran 1 test in 0.031s
FAILED (failures=1)
##teamcity[testSuiteFinished timestamp='2018-05-20T23:59:59.977' locationHint='python<C:\testSiteDemoTests>://site_tests.SiteTests' name='SiteTests' nodeId='2' parentNodeId='1']
##teamcity[testSuiteFinished timestamp='2018-05-20T23:59:59.977' locationHint='python<C:\testSiteDemoTests>://site_tests' name='site_tests' nodeId='1' parentNodeId='0']
This message shows up in exception's formatting in Python 3 if another exception was raised in an exception handler or finally clause for the first one:
A similar mechanism works implicitly if an exception is raised inside an exception handler or a finally clause: the previous exception is then attached as the new exception’s __context__ attribute
Setting a breakpoint on the test in PyCharm, then stepping further into the machinery shows where this second exception is thrown. _jb_unittest_runner patches the assert methods in unittest:
PyCharm Community Edition\helpers\pycharm\_jb_unittest_runner.py:
from teamcity import unittestpy
PyCharm Community Edition\helpers\pycharm\teamcity\unittestpy.py:
def run(self, test):
<...>
patch_unittest_diff(subtest_filter)
<...>
PyCharm Community Edition\helpers\pycharm\teamcity\diff_tools.py:
def patch_unittest_diff(<...>):
old = unittest.TestCase.assertEqual
def _patched_equals(self, first, second, msg=None):
try:
old(self, first, second, msg)
return
except AssertionError as native_error:
if not test_filter or test_filter(self):
error = EqualsAssertionError(first, second, msg)
if error.can_be_serialized():
raise error
raise native_error
unittest.TestCase.assertEqual = _patched_equals
Related
This is my current code:
import psutil
count = 0
while count < 3000000000:
for process in psutil.process_iter():
if process.name().lower() == 'chrome.exe':
print(process)
process.terminate()
count = count + 1
else:
print('no')
print(count)
its scanning all of he processes running. It then crashes when it scans:
' WindowsInternal.ComposableShell.Experiences.TextInput.Inpu...'
This is the windows service for the on screen keyboard. I tried to stop it running however it still runs. I was wondering if i could give my python file full permissions so this stops happening.
This is the error code from the terminal:
Traceback (most recent call last):
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_common.py", line 449, in wrapper
ret = self._cache[fun]
AttributeError: _cache
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 679, in wrapper
return fun(self, *args, **kwargs)
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_common.py", line 452, in wrapper
return fun(self)
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 766, in exe
exe = cext.proc_exe(self.pid)
PermissionError: [WinError 24] The program issued a command but the command length is incorrect: '(originated from NtQuerySystemInformation)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/benmi/PycharmProjects/HelloWorld/TerminateProgram.py", line 5, in
if process.name().lower() == 'chrome.exe':
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil__init__.py", line 630, in name
name = self._proc.name()
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 750, in name
return os.path.basename(self.exe())
File "C:\Users\benmi\PycharmProjects\HelloWorld\venv\lib\site-packages\psutil_pswindows.py", line 681, in wrapper
raise convert_oserror(err, pid=self.pid, name=self._name)
psutil.AccessDenied: psutil.AccessDenied (pid=8612)
I have this exception defined:
class ArgumentsException(Exception):
"""Exception that is raised when incorrect arguments are used."""
pass
Now I run my test where I run my program through sh package. And when it raises this expected exception, sh catches that exception itself and then reraises his own exception. Is there a way for me to check if my original exception was raised somehow?
For example when I run this code (this code is expected to raise that exception):
sh.python3(
self.main_py_path,
self.live_cfg_path,
self.workflow_cfg_path)
I get this exception instead:
Traceback (most recent call last):
File "/home/oerp/src/devops-tools/tests/test_main.py", line 153, in test_full_workflow_1
self.workflow_cfg_path)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 1427, in __call__
return RunningCommand(cmd, call_args, stdin, stdout, stderr)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 774, in __init__
self.wait()
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 792, in wait
self.handle_command_exit_code(exit_code)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 815, in handle_command_exit_code
raise exc
sh.ErrorReturnCode_1:
RAN: /usr/bin/python3 /home/oerp/src/devops-tools/main.py /home/oerp/src/devops-tools/tests/configs/__live__.py /home/oerp/src/devops-tools/tests/configs/__workflow__.py
STDOUT:
STDERR:
Traceback (most recent call last):
File "/home/oerp/src/devops-tools/main.py", line 204, in <module>
state = _get_state(args.state, ignore_state=args.ignore_state)
File "/home/oerp/src/devops-tools/main.py", line 68, in _get_state
"__state__.py file must be provided if --ignore-state flag "
exceptions.ArgumentsException: __state__.py file must be provided if --ignore-state flag is not used.
Well I can do something like:
self.assertTrue('ArgumentsException' in str(e.stderr))
But maybe there is more elegant way to check my exception?
On my iMac, the debugger, running a faulty python script (on MAC OS Sierra), always points to the first active line of code as cause of an exception, while when launching without debugger the correct line is identified. Does anybody have an idea why this may occur and how to fix it?
Here's a simple example with a "File not found" exception:
The script exception_test.py:
1 # Some dummy lines...
2 a=1
3 b=2
4 c=a+b
5 # Lines casuing the exception:
6 with open("filename","r") as fid:
7 lines=fid.readlines()
When run without debugger, as in python exception_test.py it yields
Traceback (most recent call last):
File "exception_test.py", line 6, in <module>
with open("filename","r") as fid:
IOError: [Errno 2] No such file or directory: 'filename'
identifying the correct line, i.e. line6,
while python -m pdb exception_test.py and successive c to continue yields
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1314, in main
pdb._runscript(mainpyfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pdb.py", line 1233, in _runscript
self.run(statement)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 400, in run
exec cmd in globals, locals
File "<string>", line 1, in <module>
File "exception_test.py", line 2, in <module>
a=1
IOError: [Errno 2] No such file or directory: 'filename'
Uncaught exception. Entering post mortem debugging
indicating the first active line of code, i.e. line 2.
It is more likely an issue of cpython and pdb, not your code, pypy could print correct traceback line number.
the line_no of the frame where the exception occurred is not right under -m pdb.
after some guessing and tracing, I could narrow to this path:
sys.settrace(self.trace_dispatch)
-> trace_dispatch() -> dispatch_line() -> user_line() -> interaction()
the real root cause is still not clear.
I am new to python. And I need to run getmail. After lots of error msgs I reduced my claim to run the tests under lib/test first:
python .\test\regrtest.py -v -M 2.0Gb -uall,-bsddb,-largefile
I am getting failure msgs from some of them but at:
test test_re failed -- Traceback (most recent call last):
File "F:\python27_64\lib\test\test_support.py", line 1287, in wrapper
return f(self, maxsize)
File "F:\python27_64\lib\test\test_re.py", line 973, in test_large_search
s = 'a' * size
OverflowError: cannot fit 'long' into an index-sized integer
Warning -- files was modified by test_sax
test test_shutil failed -- Traceback (most recent call last):
File "F:\python27_64\lib\test\test_shutil.py", line 138, in test_rmtree_dont_delete_file
handle, path = tempfile.mkstemp()
File "F:\python27_64\lib\tempfile.py", line 314, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags)
File "F:\python27_64\lib\tempfile.py", line 245, in _mkstemp_inner
_set_cloexec(fd)
File "F:\python27_64\lib\tempfile.py", line 50, in _set_cloexec
flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
AttributeError: 'module' object has no attribute 'F_GETFD'
python is hangup :-(
ALL ideas are welcome
I'm fairly green with python testing, so this might be something I'm doing wrong..
When I run my tests, the test runners works fine and coverage too.. but between the two I get an assertion error:
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
To make thing more difficult, I'm trying to test a command line utility. Which means I had to tell coverage to cover subprocess calls.
I think I got this part working since coverage is now reporting a % of cover for the script that is being run. But since I got coverage working I can't get rid of the AssertionError.
Some help understanding what's wrong would be really appreciated. All my code is available on github:
repo
setup.py
run_tests
subprocess call
Quick run:
cd /tmp/ && git clone git://github.com/h3/django-duke-client.git
cd django-duke-client && chmod a+x run_tests && ./run_tests
Thanks
Update
I've run the test on a different computer and I got the same AssertionError .. Plus a new TypeError. Again the tests runs correctly and coverage also seems to work properly even with those errors..
...
Ran 9 tests in 1.324s
OK
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------
dukeclient/__init__ 53 53 2 0 4% 1-93
dukeclient/commands/__init__ 41 33 6 2 26% 1-9, 12, 14-15, 17, 24-28, 34-43, 46-63
...
Regarding the NoneType is not callable error, please find below some elements that may help you.
In your module plugintest.py from nose-1.1.2-py2.7.egg/nose/plugins/, line 174, one can read the following line :
from multiprocessing import Manager
That leads the multiprocessing.util package to be imported, and with it an exit function to be registered :
atexit.register(_exit_function)
The problem seems to be that multiprocessing.util which is loaded in plugintest is then unloaded before the _exit_function gets called, and it's function definitions by the way.
Thus, if you import it in your setup.py file :
from multiprocessing import util
The error disappear.
By the way, I had to comment some parts in the tests that were failing or change some lines of code :
the -m command does not seems to be valid ;
I had to rename duke_conf.yml to duke_conf.yml ;
the tests that checks if README.rst and LICENSE files exists are failing (don't had the time to check why) ;
Hope it helps,