183
社区成员
The Link Your Class | https://bbs.csdn.net/forums/MUEE308FZ |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/603781458 |
The Aim of This Assignment | Course Summary |
MU STU ID and FZU STU ID | 19105681 831901211 |
Preparation
Learn related knowledge about Junit Test for Java and Unittest for python. You can finish the following assignment or test Your Project.
Task
Requirements
Tips:
1.unittest assert test
import unittest, random
# Test Class
class MyClass(object):
@classmethod
def sum(self, a, b):
return a + b
@classmethod
def div(self, a, b):
return a / b
@classmethod
def retrun_None(self):
return None
# Unit Test Class
class MyTest(unittest.TestCase):
# assertEqual()
def test_assertEqual(self):
# test if a+b equals sum or not
try:
a, b = 1, 2
sum = 3
self.assertEqual(a + b, sum, 'assert failed!,%s + %s != %s' % (a, b, sum))
except AssertionError as e:
print(e)
# assertNotEqual()
def test_assertNotEqual(self):
# fix missing three lines of codes below ‘try’, test if b-a equals res or not
try:
a, b = 1, 3
res = 3
self.assertNotEqual(b - a, res, 'assert failed!,%s + %s != %s' % (a, b, res))
except AssertionError as e:
print(e)
# assertTrue()
def test_assertTrue(self):
try:
self.assertTrue(1 == 1, "False expression")
except AssertionError as e:
print(e)
# assertFalse()
def test_assertFalse(self):
# fix missing codes below ‘try’, only a line of codes needed
try:
self.assertFalse(1 != 1, "False expression")
except AssertionError as e:
print(e)
# assertIs()
def test_assertIs(self):
# test a and b are totally same
try:
a = 12
b = a
self.assertIs(a, b, "%s and %s are not same" % (a, b))
except AssertionError as e:
print(e)
# assertIsInstance()
def test_assertIsInstance(self):
# fix missing codes below ‘y=object’ to test type(x) != y, only a line of codes needed
try:
x = MyClass
y = object
self.assertIsNot(x, y, "%s and %s are not same" % (x, y))
except AssertionError as e:
print(e)
if __name__ == '__main__':
# run unittest
unittest.main()
✔result of this test:
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import Help.Calculate;
import Help.CalculateTest;
import Help.Car;
import Help.CarTest;
/*
add two lines of annotations. Implement Suite Test of CalculateTest and CarTest
Suite Test codes must next to Class SuiteTest, no shift allowed!
*/
//**************************************************************
@RunWith(Suite.class)
@Suite.SuiteClasses({ CalculateTest.class, CarTest.class})
public class SuiteTest {
}
Through this experiment, I gain a deeper understanding of the code structure used to implement the back end. It is a good chance to improve the writing ability and understanding of the code, and ultimately my testing is successful.