# Unit testing in python

There were some choices for unit test external libraries, but now unittest is a part of
import random
import unittest

from Hello import *

class TestHello(unittest.TestCase):
def setUp(self):
self.hello = Hello()

class TestHello2(unittest.TestCase):
def setUp(self):
self.hello = Hello()

self.assertEqual(self.hello.subtract(2,12), -10)

self.assertEqual(self.hello.subtract(2,13), -11)

if __name__ == '__main__':
unittest.main()

In order to execute the code, you just need to run the unit test code.
python UNIT_TEST_CODE.PY


## Things to consider

Normally, unit testing starts with a TestCase per class under the test. And for multiple TestCases, one can bundle the tests into TestSuite as you do in java.

However, with python (it seems like that) the unittest.main() searches into classes that inherit from unittest.TestCase to execute all the tests.

You see that in this example, 4 tests are executed automatically.
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK


# Unit testing using pycharm

PyCharm is a python development environment implemented in Java programming language from Jet Brains. You will find a Python plugin for IntellJ, but they are two different products.

PyCharm doesn't have automatic unit test generator, but it seems like that the company doesn't have a plan to support it.

PyCharm is smart enough that when you click the run button, it's for executing unit test.

# Doctest

Doctest provides easy way to test. This example shows the usage of it.
"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120
"""

def factorial(n):
"""Return the factorial of n, an exact integer >= 0.

If the result is small enough to fit in an int, return an int.
Else return a long.

>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]

Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
"""

import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n:  # catch a value like 1e300
raise OverflowError("n too large")
result = 1
factor = 2
while factor <= n:
result *= factor
factor += 1
return result

if __name__ == "__main__":
import doctest
doctest.testmod()

The format is just use  >>>  to indicate the code to execute, and write down the expected result including the error message.

When you want to execute it as a command line, nothing will happen as a default. You can use -v option to see the testing result.
smcho@prosseek Desktop> python example.py -v
Trying:
factorial(5)
Expecting:
120
ok
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30.1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be exact integer
ok
2 items passed all tests:
1 tests in __main__
2 tests in __main__.factorial
3 tests in 2 items.
3 passed and 0 failed.
Test passed.



## Reference

### google services test : picasa

I needed to use LaTeX script in blogpost, and this site teaches me how to do that, but I need to use google services such as picasa and google/sites for it. So, I tried.

# Show pictures from picasa in blogspot

Picasa allows me to upload pictures from my mac to cloud. I'm trying to show the pictures from picasa, however, just href'ing doesn't seem to work. File uploaded to picasa from my mac. https://lh6.googleusercontent.com/-AfYyCt4YI-c/UIbCRY1mA7I/AAAAAAAAAEg/JlgYadGokhg/w561-h421-p-k/IMG_0542.jpg What I needed was to use insert image, and instead of uploading pictures, I can like the page from the picasa. My concern is if this link is temporary or not.

You can change the picture size by modifying width and height.

## The real solution

This site gave me a hint that the site I need to check is https://picasaweb.google.com/, not http://picasa.google.com/. Now, I can embed the slide show. Or, just one picture.
 10/23/12

### Setup sequence for UNIX/LINUX using bash

Refer to this post.

### Start up sequence for emacs

For the good old days, MS-DOS has autoexec.bat, and UNIX/Linux system has .bashrc scripts or others. Then, what's the startup sequence for emacs? What files can be modified in order to do what I want to do with emacs when startup? You can refer to this article.