Saturday, March 21, 2015

Test Cases and Assertion

Hello sloggers!! This week we talked about unit tests and assertion. In class, we went over the implementation idea for minimax and I am glad that we had a similar approach. One problem I encountered when I was trying to test on Assignment2 is that, no matter how many times I try playing the game, I still feel uncertain about my algorithm. This week's lecture actually provided an answer to this problem, Danny explained how to choose the "right" test cases and the possibilities of getting them all right at the same time is extremely small. I am now confident about our minimax algorithm as it passes the test cases we discussed in class. One thing I forgot to mention in the previous post is how we had trouble dealing with lists as parameters passing between functions. This posed a dangerous and really serious problem, as any changes you make to one list affect all the others that have a same reference. We found the solution after hours of trying detect the problem and we used deepcopy to solve this problem. In lecture, Danny introduced another way to make a copy of list instead of using deepcopy, and it looks like this
L3 = [[item for item in r] for r in L1]
I find this really interesting, since if you just put one forloop for the inside list, you end up only protecting the elements in the inside list from changing. To protect the whole list, you have to do it like that.
Another new thing we learned is assertion. It is a tool that can be used to state what we know is true at a crucial point in the code or what we assumed is true at the start of a function or method.
The syntax is
assert «condition», «error_string»
So the meaning for this statement is, if condition fails,
 raise AssertionError(error_string)
I think these will definitely become handy as I am doing my last assignment. I have learned how to perform effective test cases and I will apply the knowledge to A3.

No comments:

Post a Comment