Doctest error with simple case - python

With
def show(a):
""" Shows a string
>>> show(a)
a
"""
print(a)
def test():
import doctest
doctest.testmod()
if __name__ == '__main__': test()
I am getting an error while trying to learn how a docstring works.
Both this method and running it from command line with
python -m doctest unittest.py
ends with errors.
Traceback (most recent call last):
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 19, in <module>
if __name__ == '__main__': test()
File "/home/liquid/workspace/MyPythonProject/src/unittest.py", line 16, in test
import doctest
File "/usr/lib/python3.2/doctest.py", line 2105, in <module>
class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'
Why?

Unfortunately you named your module the same as the one containing TestCase. Rename unittest.py to myunittest.py and see if it works.

Related

NameError in Python script for Cron Implementation

I am trying to perform basic Cron implementation using Python. For which, I created Cron class and tried calling the my_job() function. But, I am receiving syntax error. How can I correct the syntax error?
Python Code:
import schedule
import time
class Cron:
def my_job(self):
print('Foo')
def Start(self):
schedule.every(5).to(10).seconds.do(my_job)
while 1:
schedule.run_pending()
time.sleep(1)
A = Cron()
A.Start()
Following is exception received:
Traceback (most recent call last):
File "cron.py", line 17, in <module>
A.Start()
File "cron.py", line 11, in Start
schedule.every(5).to(10).seconds.do(my_job)
NameError: name 'my_job' is not defined

AttributeError: 'module' object has no attribute 'timeit' while doing timeit a python function

i want to timeit a python function or want to print best time it takes for executing my_function() for 12 iteration.
below is my code:
def my_function():
print "hello"
if __name__ == "__main__":
import timeit
setup = "from __main__ import my_function"
print timeit.timeit("my_function()", setup=setup,number=12)
but i am getting below errror
Traceback (most recent call last):
File "timeit.py", line 7, in <module>
print timeit.timeit("my_function()", setup=setup,number=12)
AttributeError: 'module' object has no attribute 'timeit'
anybody please help..
You named your file timeit.py, which blocks the builtin module, so import timeit is importing your own file. Name your file something else.

Executing Python doctest code

I have simple Python code that uses dockets
#!/usr/bin/python
# http://stackoverflow.com/questions/2708178/python-using-doctests-for-classes
class Test:
def __init__(self, number):
self._number=number
def multiply_by_2(self):
"""
>>> t.multiply_by_2()
4
"""
return self._number*2
if __name__ == "__main__":
import doctest
doctest.testmod(extraglobs={'t': Test(2)})
I can use it with python interpreter:
> python simple.py
However, when I execute the code from doctest module, I get this error:
> python -m doctest simple.py
**********************************************************************
File "simple.py", line 10, in simple.Test.multiply_by_2
Failed example:
t.multiply_by_2()
Exception raised:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
File "<doctest simple.Test.multiply_by_2[0]>", line 1, in <module>
t.multiply_by_2()
NameError: name 't' is not defined
**********************************************************************
1 items had failures:
1 of 1 in simple.Test.multiply_by_2
***Test Failed*** 1 failures.
Why is this difference? How to resolve this issue?
The difference is that when you execute via doctest, it is the __main__ module compared to executing directly where your script's if __name__ == '__main__' block will execute.
I don't know of a good solution other than to put all the information you need in the docstring itself:
def multiply_by_2(self):
"""
>>> t = Test(2)
>>> t.multiply_by_2()
4
"""
return self._number * 2
This will have the added benefit that users who are reading your docstrings will know what's going on ... They won't have to stumble upon your extraglobs keyword to figure out what t is and how it was initialized.

AttributeError: 'module' object has no attribute 'TestSequenceFunctions'

I'm working on unittest in python.
I'm working on a Ububtu machine from "/home/jamy/PycharmProjects/xcxzc/UnitTesting.py" directory and trying to run this following code:
import unittest
class LearningCase(unittest.TestCase):
def test_starting_out(self):
self.assertEqual(1, 1)
def main():
unittest.main()
if __name__ == "__main__":
main()
But I am getting this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "unitTesting.py", line 1, in <module>
import unittest
File "/home/jamy/PycharmProjects/xcxzc/unittest.py", line 4, in <module>
AttributeError: 'module' object has no attribute 'TestSequenceFunctions'
How can I fix this?
By naming your script unittest.py you are shadowing the built-in unittest module.

Python unittest - ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest

I have a very simple setup that uses unittest and I am getting an error that I don't understand.
# mytestcase.py
import unittest
class MyTestCase(unittest.TestCase):
def test_one(self):
self.assertTrue(True)
def test_two(self):
self.assertTrue(False)
def initialize():
return MyTestCase()
if __name__ == '__main__':
unittest.main()
If I execute the above file, I get the following result, which I expect and understand:
> python mytestcase.py
.F
======================================================================
FAIL: test_two (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "mytestcase.py", line 7, in test_two
self.assertTrue(False)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1)
But I want to run these tests another way, from my_test_manager.py:
# my_test_manager.py
import mytestcase
test_case = mytestcase.initialize()
test_suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
test_suite_result = unittest.TestResult()
test_suite.run(test_suite_result)
for err in test_suite_result.errors:
print err
for fail in test_suite_result.failures:
print fail
But if I try to run this file, it crashes as follows:
> python my_test_manager.py
Traceback (most recent call last):
File "my_test_manager.py", line 3, in <module>
test_case = mytestcase.initialize()
File "/Users/Jon/dev/test-tools/practice/mytestcase.py", line 11, in initialize
return MyTestCase()
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 191, in __init__
(self.__class__, methodName))
ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest
You do not need to create an instance; return the MyTestCase class itself:
def initialize():
return MyTestCase
Thanks it works like u suggested.
from mytestcase import MyTestCase
import unittest
test_suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
test_suite_result = unittest.TestResult()
test_suite.run(test_suite_result)
for err in test_suite_result.errors:
print("hello")
for fail in test_suite_result.failures:
print("no hello")

Categories