Friday, November 26, 2010

SBR600 Project "fedpkg Test Suite" -- release 0.2

For testing fedpkg library functions, I wrote 4 tests. Nothing is significant. It's really about "finding the way in the wood". I mean, even I got myself familiar with Python syntax, when come to a real program, I still can't understand half of it. So it took some time to find the clue in the program. Another thing is to learn "unittest". I scrach the surface of it but still need time to know how to use the functions.

I put my test file in my matrix account. I took the __init__.py file from fedpkg and renamed it to pyfedpkg.py. I have two tests on clone, a test on commit and a test on _run_command. They are quite trivel. I will continue to work on them toward my next release. The output of my test is pasted as following.

[zwang98@dell fedpkg]$ ./test_fedpkg.py
Cloning into nled...
remote: Counting objects: 82, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 82 (delta 38), reused 82 (delta 38)
Receiving objects: 100% (82/82), 8.20 KiB, done.
Resolving deltas: 100% (38/38), done.
nled/nled.spec
.Cloning into bind...
ssh_exchange_identification: Connection closed by remote host
fatal: The remote end hung up unexpectedly
user is not valid
.fatal: Not a git repository (or any parent up to mount parent )
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
commit must have an argument
.nled/passwd
.
----------------------------------------------------------------------
Ran 4 tests in 7.279s

OK

Thursday, November 4, 2010

SBR600 Project "fedpkg Test Suite" -- release 0.1

I decide to do the release 0.1 of my project although there are not as many functions being tested as I planned to. I believe that the stage one of fedpkg Test Suite is mainly to learn Python. I spent, cumulatively, about 3 - 4 weeks to get myself familiar with the syntax of Python.

The stage one of my project is to write some simple functions and use nose to run the test. As exercise of programming Python, I wrote three functions on list. List is relatively simpler than dictionary. Maybe I will add one or two functions of dictionary to run the nose test on them. But the nose principle is the same. Just coding on dictionary should be more complex (so, it's about the skill of Python).

I wrote three functions on list. They are all very simple. One is to remove negative items from a number list. I blogged some details in my last post. Another function is to find duplicate items in a list and return a set of these duplicates. The third function is bubble sort.

The test functions are in the same file as the functions being tested. Because they all very small, it's easier to debugging this way. The name of the test function has to start with test. I mainly use keyword assert to test the results.

From here, I need to dive in the fedpkg code. I also need to get familiar with the functionality of fedpkg, which probably need certain knowledge of Git. Another thing I am still not clear is the relationship between nose and unittest framework. I thought they are the same at beginning, but probably they are not.

I dump the test code in my matrix account:
http://matrix.senecac.on.ca/~zwang98/nosetest.html

Wednesday, November 3, 2010

Learning Python

My project for SBR600 is fedpkg Test Suite. For 0.1 release, I plan to write some simple functions in manipulating list, and then use nose to test the functions. The first function I try to write is a simple exercise: given a list of numbers, the function will remove the negative items.

The first try

def rem_neg(list):
    for i in list:
        if list[i] < 0:
            list.remove(list[i])

before I use nose to test, I did some regular test. Give a list [1, 2, -1, 3, -2], the result will be [1,2,3]. Not bad. But when I use [5,-1,-2,3,4,-8], the result is [5-2,3,4]. So the function is not right. I went to check the book and realized that when a list remove an item, the rest will shift to the left. Therefore, the for loop missing the second negative number if two negative numbers next to each other.

The second try

I tried to find a way to work around the shifting problem. How about do the loop backward? Then the shift won't cause problem anymore.

def rem_neg(list)
    for i in range(len(list)-1, 0, -1):
        if list[i] < 0:
            list.remove(list[i])

I tested the previous example. It worked. But I should test more examples. The text book I am read said to use special case or extreme case to test. When I test list [-1], it failed again. The result is [-1]. So the function didn't reach the first item (It's the last if we consider the loop goes backward). It's typical "off by 1" error in programming. My for loop range should be (len(list)-1, -1, -1). The range function always stop 1 before the last item.

Come to nose

Now I put the function to nose test.

import nose

def rem_neg(list)
    for i in range(len(list)-1, -1, -1):
        if list[i] < 0:
            list.remove(list[i])
def test_rem_neg():
    assert rem_neg([-1]) == []

if __name__ == '__main__' :
    nose.runmodule()

The test failed. It took a while for me to figure out what is wrong. The function rem_neg(list) return none. I wrote the function this way just to follow the style of list built-in functions. This is: the original list get modified and nothing returned (except the pop function).

Come next: 0.1 release

This is just the first example I plan to use for the nose test. I will test more examples in two days and finish my 0.1 release.