•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,809 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,524 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 1489 | Replies: 6
![]() |
•
•
Join Date: Oct 2006
Location: New York City
Posts: 2,553
Reputation:
Rep Power: 7
Solved Threads: 1
Is there an option of some sort in the Dr. Python interpreter that would allow one to re-run the same output results after making adjustments to the code.
Example:
I run an example of source code and receive an output result. I make changes to the code. I now am able to run the same code (with the same output) to see how the new code affects a particular output result.
This just came to mind today while doing testing on a personal Python project. This would really save time and allow me to see reactions to adjusted code immediatly rather than hoping to come upon the same bug by running code again and again manually.
I am sure something like this exists in compilers in general, I imagine, but I have never made use of it.
Thanks for any help in advance.
sharky_machine
Example:
I run an example of source code and receive an output result. I make changes to the code. I now am able to run the same code (with the same output) to see how the new code affects a particular output result.
This just came to mind today while doing testing on a personal Python project. This would really save time and allow me to see reactions to adjusted code immediatly rather than hoping to come upon the same bug by running code again and again manually.
I am sure something like this exists in compilers in general, I imagine, but I have never made use of it.
Thanks for any help in advance.
sharky_machine
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Do you mean some kind of automated testing, where you can pass the same input to the code each time?
There are ways of doing that that are really sophisticated, but I usually just wrap the code in some kind of test() function that tests each class or function.
Writing the test() is a bear, but then you can just run the file, type test(), and relax. I mean, go and fix the bugs. At least in my case.
:lol:
Jeff
There are ways of doing that that are really sophisticated, but I usually just wrap the code in some kind of test() function that tests each class or function.
Writing the test() is a bear, but then you can just run the file, type test(), and relax. I mean, go and fix the bugs. At least in my case.
:lol:
Jeff
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation:
Rep Power: 9
Solved Threads: 173
You could write a small Python program that will run your script repeatedly (put a time delay into the loop) with arguments you give it ...
Then write your MyScript.py in such a way that the output goes to a file with a time/date stamp in the filename. I think there is a suggestion how to do this in the "Starting Python" sticky. Finally compare the files with another Python program.
python Syntax (Toggle Plain Text)
# run an external program from within Python code with # subprocess.call(["program-name", "arg1", "arg2", ...]) import subprocess # part of a loop subprocess.call(["C:/Python25/Python.exe", "MyScript.py", "arg1", "arg2"])
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation:
Rep Power: 9
Solved Threads: 173
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
Intrigued, I coded up a simple unittest example.
Here's the output with all Rectangle methods broken:
and with all Rectangle methods set to their correct code:
My question: is that last bit normal? Why the complicated 'sys.exit(not result.wasSuccessful())'?
Anyways, the OK is the sign that all is well with the world.
Jeff
Python Syntax (Toggle Plain Text)
import unittest # The class to test class Rectangle(object): def __init__(self, w, h): self.width = w self.height = h def get_width(self): return self._w def set_width(self, w): if w == None: raise ValueError self._w = w width = property(get_width, set_width) def get_height(self): return self._h def set_height(self, h): if h == None: raise ValueError self._h = h height = property(get_height, set_height) def area(self): return self.height * self.width class TestRectangle(unittest.TestCase): # the magic occurs here # by subclassing TestCase def setUp(self): self.test_cases = [(2,3),(0,5),(-2.5,6)] self.test_widths = [2,0,-2.5] self.test_heights = [3,5,6] self.test_areas = [6,0,-15] # the last element could be the subject of debate: # Should rectangles be allowed to have signed area and sides? # In the interest of physics, I decided Yes' (think flux here), but # here in the test suite is the place that debate amongst the # programming team could occur. self.error_cases = [(None,3),(5,None),(None,None)] self.error_results = [ValueError,ValueError,ValueError] def testHeight(self): self.Rects = [Rectangle(*x) for x in self.test_cases] self.assertEqual([x.height for x in self.Rects], self.test_heights) def testWidth(self): self.Rects = [Rectangle(*x) for x in self.test_cases] self.assertEqual([x.width for x in self.Rects], self.test_widths) def testArea(self): self.Rects = [Rectangle(*x) for x in self.test_cases] self.assertEqual([x.area() for x in self.Rects], self.test_areas) def testErrors(self): errors = dict(zip(self.error_cases, self.error_results)) for x in errors: w,h = x self.assertRaises(errors[x], Rectangle, w, h) if __name__ == "__main__": unittest.main() #automagically creates an instance of my # TestRectangle class, and runs each method.
Here's the output with all Rectangle methods broken:
FFEE
======================================================================
ERROR: testHeight (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 50, in testHeight
self.assertEqual([x.height for x in self.Rects], self.test_heights)
File "C:/Python24/unittest_test.py", line 21, in get_height
return self._h
AttributeError: 'Rectangle' object has no attribute '_h'
======================================================================
ERROR: testWidth (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 55, in testWidth
self.assertEqual([x.width for x in self.Rects], self.test_widths)
File "C:/Python24/unittest_test.py", line 10, in get_width
return self._w
AttributeError: 'Rectangle' object has no attribute '_w'
======================================================================
FAIL: testArea (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 60, in testArea
self.assertEqual([x.area() for x in self.Rects], self.test_areas)
AssertionError: [None, None, None] != [6, 0, -15]
======================================================================
FAIL: testErrors (__main__.TestRectangle)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 67, in testErrors
self.assertRaises(errors[x], Rectangle, w, h)
AssertionError: ValueError not raised
----------------------------------------------------------------------
Ran 4 tests in 0.015s
FAILED (failures=2, errors=2)
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 71, in -toplevel-
ut.main()
File "C:\Python24\lib\unittest.py", line 759, in __init__
self.runTests()
File "C:\Python24\lib\unittest.py", line 797, in runTests
sys.exit(not result.wasSuccessful())
SystemExit: Trueand with all Rectangle methods set to their correct code:
....
----------------------------------------------------------------------
Ran 4 tests in 0.015s
OK
Traceback (most recent call last):
File "C:/Python24/unittest_test.py", line 68, in -toplevel-
unittest.main()
File "C:\Python24\lib\unittest.py", line 759, in __init__
self.runTests()
File "C:\Python24\lib\unittest.py", line 797, in runTests
sys.exit(not result.wasSuccessful())
SystemExit: FalseMy question: is that last bit normal? Why the complicated 'sys.exit(not result.wasSuccessful())'?
Anyways, the OK is the sign that all is well with the world.
Jeff
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- Starting Python (Python)
- i need this for running in a compiler (C++)
- running a process from a python program (Python)
- How to use 'top' using Python (Python)
- Do I need to upgrade Python? (Python)
Other Threads in the Python Forum
- Previous Thread: Having 2 versions of Python
- Next Thread: Thread / Timers / Pinging... Help :-s



Linear Mode