This week is about testing. It is a really tough and
complicated, and I am still very confused about it so that I cannot tell too
much on it.
To start with testing, we have to know the reason we
test. We test because we don’t know if our codes are running as we expected. So
we have to input the expected results and see if it is same as the real answer.
To explain it better, I will make a general example, or
we can call it the model.
Firstly, we write ‘ import unittest’ on the top.
Then we create a class, for instance, class TEST, with (unittest.Testcase)
after it.
Then we write method.
The first method will be
def setup(self). In this function, all the codes will be operated before
every testcase starts. We can initialize all the classed and variables here,
and to access in other methods, all the initialized data are supposed to start
with self., or be global variables.
The second method should be def tearDown(self). Similarly, all codes in this function will
be operated before the every testcase starts, but the difference is, they will
also be operated after the testcase stops. Since in the testcase, the variables
starts with self. that we defined or other global variables will probably be
modified by testcase for testing the results, we use tearDown function to make
them nothing( self.blahblah = None), like throwing trash.
Finally, we write the key part of the test. We need a
method called def test_test_case_name(self), which is allowed to be named by
ourselves. Under this method, we need to write four lines of code: Actual =
______, which is the content we test
Expected =
_______, which is the result we expected
Error = _____, which
is a hint to let us know that there is an error
assert actual ==
expected, error # maybe >= or <=
Above are my thoughts about test, maybe now it is still
childish, but I will try to learn it
better.
没有评论:
发表评论