codelucas/newspaper — pull request #45 by omarkhan (2014-03-16 15:30:29+00:00)
Title: Use unittest assert methods

This patch replaces the `assert ...` lines in `unit_test.py` with [the assert methods from python's unittest library](http://docs.python.org/2/library/unittest.html#assert-methods). This makes the tests give useful error messages when they fail.

Incidentally, the tests do fail when I run them (see below). I'd like to hack on the library, but I'd be nice to have the tests pass first. Could you point me in the right direction?

```
$ python tests/unit_tests.py
testing function 'test_config_build'
[OK] in 'test_config_build' 0.02 sec
.	testing function 'test_chinese_fulltext_extract'
Building Trie..., from /Users/omar/dev/newspaper/newspaper/packages/jieba/dict.txt
loading model from cache /var/folders/t9/2_t3ywb917772b0lkp0lvt900000gn/T/jieba.cache
loading model cost 1.59256482124 seconds.
Trie has been built succesfully.
Etesting source unit
testing function 'source_url_input_none'
[OK] in 'source_url_input_none' 0.00 sec
testing function 'test_cache_categories'
[OK] in 'test_cache_categories' 2.53 sec
testing function 'test_source_build'
F	testing function 'test_encode_val'
[OK] in 'test_encode_val' 0.00 sec
testing function 'test_smart_unicode'
[OK] in 'test_smart_unicode' 0.00 sec
testing function 'test_smart_str'
[OK] in 'test_smart_str' 0.00 sec
.testing url unit
testing function 'test_valid_urls'
[OK] in 'test_valid_urls' 0.00 sec
.testing article unit
testing function 'test_url'
[OK] in 'test_url' 0.00 sec
testing function 'test_download_html'
[OK] in 'test_download_html' 2.96 sec
testing function 'test_pre_download_parse'
You must download() an article before parsing it!
[OK] in 'test_pre_download_parse' 0.00 sec
testing function 'test_parse_html'
we now have 48 images
Ftesting API unit
testing function 'test_article_build'
[OK] in 'test_article_build' 8.26 sec
testing function 'test_hot_trending'
[OK] in 'test_hot_trending' 1.83 sec
testing function 'test_popular_urls'
[OK] in 'test_popular_urls' 0.00 sec
.
======================================================================
ERROR: runTest (__main__.MultiLanguageTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit_tests.py", line 366, in runTest
self.test_chinese_fulltext_extract()
File "tests/unit_tests.py", line 36, in run
method(*args, **kw)
File "tests/unit_tests.py", line 375, in test_chinese_fulltext_extract
article.parse()
File "/Users/omar/dev/newspaper/tests/../newspaper/article.py", line 214, in parse
self.fetch_images()
File "/Users/omar/dev/newspaper/tests/../newspaper/article.py", line 222, in fetch_images
self.set_meta_img(meta_img_url)
File "/Users/omar/dev/newspaper/tests/../newspaper/article.py", line 423, in set_meta_img
self.set_top_img(src_url)
File "/Users/omar/dev/newspaper/tests/../newspaper/article.py", line 428, in set_top_img
if s.satisfies_requirements(src_url):
File "/Users/omar/dev/newspaper/tests/../newspaper/images.py", line 221, in satisfies_requirements
dimension = fetch_image_dimension(img_url, self.useragent, referer=self.url)
File "/Users/omar/dev/newspaper/tests/../newspaper/images.py", line 158, in fetch_image_dimension
return fetch_url(url, useragent, referer, retries, dimension=True)
File "/Users/omar/dev/newspaper/tests/../newspaper/images.py", line 120, in fetch_url
p.feed(new_data)
File "/Users/omar/dev/newspaper/venv/lib/python2.7/site-packages/PIL/ImageFile.py", line 383, in feed
if flag or len(im.tile) != 1:
TypeError: object of type 'NoneType' has no len()

======================================================================
FAIL: runTest (__main__.SourceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit_tests.py", line 136, in runTest
self.test_source_build()
File "tests/unit_tests.py", line 36, in run
method(*args, **kw)
File "tests/unit_tests.py", line 160, in test_source_build
self.assertEqual(s.description, DESC)
AssertionError: u'CNN.com International delivers breaking news from across the globe and information on the latest top stories, business, sports and entertainment headlines. Follow the news as it happens through: special reports, videos, audio, photo galleries plus interactive maps and timelines.' != 'CNN.com delivers the latest breaking news and information on the latest top stories, weather, business, entertainment, politics, and more. For in-depth coverage, CNN.com provides special reports, video, audio, photo galleries, and interactive guides.'

======================================================================
FAIL: runTest (__main__.ArticleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/unit_tests.py", line 55, in runTest
self.test_parse_html()
File "tests/unit_tests.py", line 36, in run
method(*args, **kw)
File "tests/unit_tests.py", line 104, in test_parse_html
self.assertEqual(len(self.article.imgs), LEN_IMGS)
AssertionError: 48 != 47

----------------------------------------------------------------------
Ran 7 tests in 53.733s

FAILED (failures=2, errors=1)
```