Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread Steven D'Aprano
On Fri, Jan 31, 2014 at 02:03:13PM +, James Chapman wrote: > On the note of what doesn't need a test... > The question of coverage always comes up when unit testing is > mentioned and I read an interesting blog article once about it. It > basically said: Assume you have 85% coverage on a progr

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread James Chapman
Thanks Steven! You've raised a few valid points, mostly that the run_forever method should be broken up. I like the principle of a method doing just one thing and for whatever reason I didn't apply that thinking to this method as it's the master loop (even though it does nothing). So for starters

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread Steven D'Aprano
On Fri, Jan 31, 2014 at 11:31:49AM +, James Chapman wrote: > Hello tutors > > I've constructed an example which shows a problem I'm having testing a real > world program and would like to run it past you. [...] > class Infinite_Loop_Tutor_Question(object): > def run_forever(self): >

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread James Chapman
Hmm... Here is an example of how I'm currently trying to test it: test_tutor_question.py - # -*- coding: utf-8 -*- import unittest import mock from tutor_question import Infinite_Loop_Tutor_Question class Test_Infinite_Loop_Tutor_Question(unittest.TestCase): def

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread eryksun
On Fri, Jan 31, 2014 at 6:31 AM, James Chapman wrote: > try: > while self.attribute: > time.sleep(1) > except KeyboardInterrupt: > ... > > My unit test could then set the attribute. However I'd still have the > problem of how I get from the unit test line that fires up the method t

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread Steven D'Aprano
On Fri, Jan 31, 2014 at 01:10:03PM +0100, spir wrote: > I don't know whether one can interrupt while sleeping py> from time import sleep py> sleep(60*60*24*365) # 1 year Traceback (most recent call last): File "", line 1, in KeyboardInterrupt Yes you can. -- Steven ___

Re: [Tutor] Unit testing infinite loops

2014-01-31 Thread spir
On 01/31/2014 12:31 PM, James Chapman wrote: try: while self.attribute: time.sleep(1) except KeyboardInterrupt: Maybe I'm missing apoint or reasoning wrongly, but I'd rather do: while self.attribute: try: time.sleep(1) except KeyboardInterrupt: ... or something lik

[Tutor] Unit testing infinite loops

2014-01-31 Thread James Chapman
Hello tutors I've constructed an example which shows a problem I'm having testing a real world program and would like to run it past you. tutor_question.py -- # -*- coding: utf-8 -*- import sys import threading import time class Time_Printer(threading.Thread)