Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [002] Ian Ozsvald
Member: 106 months
Authored: 181 videos
Description: I am the co-founder of ShowMeDo (see http://showmedo.com/about), author of `The Screencasting Handbook <http://thescreencastinghandbook.com>`_ and the founder of the professional screencast production company `ProCasts <http://procasts.co.uk>`_: .. image:: http://procasts.co.uk/media/procasts_sma ...

Starting with Unit-tests [ID:250] (9/17)

in series: Python 101 - easygui and csv

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Unit-tests are an invaluable development tool and should be a part of every developer's toolkit. Not only do they help us to test that our code works as expected, they also help us to write good code in the first place and point out errors that are introduced later in the development cycle.

Here we test that the nosetests environment is installed and running before we go on to write tests for our code in the following episodes.

We also look at this special piece of code 'if __name__ == '__main__':' which lets us unit-test just our functions.

Assumptions: nosetests is already installed on your machine. This wiki page describes how to install nosetests. The Python Development series also covers the use of nosetests in this episode.

Task: make testCSVReader.py and test that nosetests works

Created May 2007, running time 7 minutes.

#import csvReader

def test1():
    pass

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 418 (since July 30th)
  • Plays in last week: 0
  • Published: 88 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

Without these video tutorials I am quite certain I would not have learned about several of these tools such as 'nosetests' for some time - and it would take quite a lot longer to figure out how to use them. Thanks.


Starting with Unit-tests (9/17)

1:20 start names with test e.g. testCsvReader

2:50 use 1/0 (one divided by zero) for test failure

5:30 if __name__ == "__main__": to suppress function calls

Resources:

http://peak.telecommunity.com/DevCenter/EasyInstall

http://somethingaboutorange.com/mrl/projects/nose/0.11.1/


thanks. good explanation of TDD


beaon,

I'll jump in and answer your question. __name__ is one of Python's many special names. In code that is imported from another program and ran, __name__ is the name of the module, but it is '__main__' when it is the program which was ran.

One common thing done is to put the code that runs the program in a function:

def main():

.... code here

if __name__ == '__main__':

main()

Thus, if desired, we can run the program when it is imported ( modulename.main() ) and it also runs when this is program ran.


This is great,

Can you clarify how the __name__ variable works?

Does the __name__ variable take the value of the .py file that ran it? How does it match __main__ only when csvReader.py is run? Any clarification or links to documentation you can provide would be rockin. Thanks!


This is great,

Can you clarify how the __name__ variable works?

Does the __name__ variable take the value of the .py file that ran it? How does it match __main__ only when csvReader.py is run? Any clarification or links to documentation you can provide would be rockin. Thanks!


I have tried to set up nose test, but I fail every time. I tried looking at Ian's introduction to unit testing in the Python on XP series, but no luck. Any quick hints? P.s. I am running on Win XP.


Since Python 2.6 has the unittest module, is it better to use unittest if one has Python 2.6 rather than nosetests?


Once again python frustrates until I figure out that I didn't properly indent the code. Great tool, great lesson!


Hi Erik. I've replied via email, I think they've checked in a bad build of 'nose'. Your install method (easy_install nose) is correct and I used it just a few weeks ago on my Windows XP laptop.

Let's wait a day and see if they checkin a better build, if not I'll have a look around on your behalf.

Ian.


I got completely lost in the easy_install and nose event


Hi Ryan...the video here runs for 7:32. Perhaps you have a badly cached copy? You might want to try flushing your cache (in Firefox use Tools|Clear Private Date) so that the video-player's private cache is cleared.

Re. a module/script - a 'script' *is* a 'module'. Python just calls a file containing Python code a 'module'. It is just a naming convention.

Ian.


Now we are getting someone - some really intermediate/advanced computer programmer (in my opinion anyways). Unit-test seem like an excellent way to test codes samples. Just wondering, how do you make a script a module, or does it do it automatically.

The video seems to cut off around the 5 minute mark.


Sorry, my mistake, the link in the above description was wrong. Now it correctly points at 'Unit Tests for Dependable Code':

http://showmedo.com/videos/video?name=pythonOzsvaldPyNewbie8&fromSeriesID=49

As you found, the instructions are in the wiki here:

http://wiki.showmedo.com/index.php/PythonOzsvaldNewbiesXPSeries#Installing_nosetests

Ian.


the link to how to install nosetests would be where? Used google and found this -

http://wiki.showmedo.com/index.php/PythonOzsvaldPyNewbieSeries


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Ian

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com