This is my last post for CSC148 Winter 2014. In today's class, Danny quickly went over all the stuff that we should know for the final exam. I think the only thing that I didn't pay attention and didn't cover in my post is unittest. This explains why I passed all my example cases, but there were still some errors and failures in my assignments. I will share a typical UnitTest example from my lab work with you. In the end, I will talk about my overall thinking and growth this term.
UnitTest:
When I first unittest, I feel I don't want to use it. We need to write a more complicated code in order to test our function, which can be tested by just typing in the shell or using doctest.
Now after I suffered some inexplicable errors and failures in my assignments, I understand why we need unittest. But I am not saying that I stopped using doctest. I use UnitTest to test my code, and use doctest to test my docstring examples. We made a big mistake in the past. We should not test our code correctness based on our docstring example, but we can check that our examples are correct based on our code. When testing a code, we need to thoroughly test every case instead of test how does our example perform. For many programmers, the unittest is written before the actual code.
unittest for class:
Following is the unittest we wrote for Motorized class in lab3:
1. import unittest module
2. import the thing (class) that we want to test
3. write setUp first
4. write thorough test cases for each class's method
(if there is error case, test it as well)
import unittest
from motorized import Motorized, NotEnoughFuelError
class TestMotorized(unittest.TestCase):
"""Example unittest test methods for motorized."""
def setUp(self):
self.motor = Motorized(4, 100, 5)
def testFillUp(self):
self.motor.fuel_level = 20
self.motor.fill_up()
self.assertTrue(self.motor.fuel_capacity == self.motor.fuel_level)
def test_correct_TravelTo(self):
self.motor.travel_to((1000.0, 500.0))
self.assertEqual(25.0, self.motor.fuel_level)
def test_error(self):
with self.assertRaises(NotEnoughFuelError):
self.motor.travel_to((10000.0, 20000.0))
if __name__ == '__main__':
unittest.main(exit=False)
unittest for function:
The only difference between function unittest and class unittest is that function unittest does not need to setup a certain type object first. We thoroughly write test cases for the function as same as we did for class's each method.
-----------------------------------------------------------end-------------------------------------------------------------------
Well, well...
Time flies~ Now it's the end of the year
I've found slog is interesting, though I know some of you really hate slog ;)
This is my first time to manage a slog and it's in English. I am very proud that I insist on and I wrote out everything that I wanted to say and that I really learned from this course.
I wonder if there was anyone except myself has seen this slog, haha, I guess not. But I indeed enjoyed it and it helped me through my hard times and finally, I mean before the final exam, I did well.
It's also my first time to experience computer programming. Maybe it's an entry-level csc course, so I think it's interesting and not as scary as I heard before. We got lots of supplemental study materials and there were many practices as well. CSC also has very helpful and respectful professors, and I have to thank my TA for answering all the random and weird questions that I had.
I made many friends in CSC108 and CSC148, a lot more than in my other classes, maybe it's due to lots of group work. BUT NO ONE COMMENT ON MY SLOG!!!
Anyway~
Good luck on the final exam and Have a great next year!
(I just can't wait for summer!)
-----------------------------------------------------------end-------------------------------------------------------------------
Well, well...
Time flies~ Now it's the end of the year
I've found slog is interesting, though I know some of you really hate slog ;)
This is my first time to manage a slog and it's in English. I am very proud that I insist on and I wrote out everything that I wanted to say and that I really learned from this course.
I wonder if there was anyone except myself has seen this slog, haha, I guess not. But I indeed enjoyed it and it helped me through my hard times and finally, I mean before the final exam, I did well.
It's also my first time to experience computer programming. Maybe it's an entry-level csc course, so I think it's interesting and not as scary as I heard before. We got lots of supplemental study materials and there were many practices as well. CSC also has very helpful and respectful professors, and I have to thank my TA for answering all the random and weird questions that I had.
I made many friends in CSC108 and CSC148, a lot more than in my other classes, maybe it's due to lots of group work. BUT NO ONE COMMENT ON MY SLOG!!!
Anyway~
Good luck on the final exam and Have a great next year!
(I just can't wait for summer!)