发现了1点,继承自unittest.TestCase 的subclass 不能重写__init__()方法。
setUp()和tearDown():会被每个test函数调用。每个test函数是按照字母排序,所以我在test_run_process后用了test_s2.
被测试的函数定义在此:
-
-
-
class Calculate:
-
def add(self,x,y):
-
if type(x)==int and type(y)==int:
-
return x+y
-
else:
-
raise TypeError("Invalid type: {} and {}".format(type(x),type(y)))
test文件在这里:
-
import numbers
-
import unittest
-
from calculate import Calculate
-
import sys
-
import subprocess
-
import shutil
-
import os
-
import os.path
-
import time
-
import filecmp
-
-
class TestCalculate(unittest.TestCase):
-
def setUp(self):
-
self.calc=Calculate()
-
-
@unittest.skip("demonstrating skipping")
-
def test_nothing(self):
-
self.fail("should not happen")
-
-
@unittest.skipIf(sys.version_info<(3,6),"not supported in this library version")
-
def test_format(self):
-
# Tests that work for only a certain version of the library
-
pass
-
-
@unittest.skipUnless(sys.platform.startswith('win'),"requires windows")
-
def test_windows_support(self):
-
#windows specific testing code
-
pass
-
-
-
def test_add_method_returns_correct_result(self):
-
self.assertEqual(4,self.calc.add(2,2))
-
-
def test_add_method_raises_typerror_if_not_int(self):
-
self.assertRaises(TypeError,self.calc.add,"Hello","world")
-
-
def test_assert_raises(self):
-
with self.assertRaises(AttributeError):
-
[].get
-
-
#assertEqaul(x,y,msg=None)
-
def test_assert_equal(self):
-
self.assertEqual(1,2,msg="we not are equal")
-
-
#assertAlsmotEqual(x,y,places=None,msg=None,delta=None)
-
def test_assert_almost_equal_delta_0_5(self):
-
self.assertAlmostEqual(1,1.2,delta=0.5,msg="we are not almost equal")
-
-
def test_assert_almost_equal_places(self):
-
self.assertAlmostEqual(1,1.00001, places=4)
-
-
#assertRaises(exception,method,arguments,msg=None)
-
def test_assert_raises_2(self):
-
self.assertRaises(ValueError,int, 'a')
-
-
#assertDictContainsSubset is deprecated in python 3.2
-
#assertDictContainsSubset(expected, actual, msg=None)
-
@unittest.skipIf(sys.version_info>(3,2),"not supported in this library version")
-
def test_assert_dict_contains_subset(self):
-
expected={'a':'b',"k":"g"}
-
actual={'a':'b','c':'d','e':'f'}
-
self.assertDictContainsSubset(expected,actual,msg="not contain")
-
-
#assertDictEqual(d1,d2,msg=None)
-
def test_assert_dict_equal(self):
-
expected={'a':'b',"k":"g"}
-
actual={'a':'b',"k":'g'}
-
self.assertDictEqual(expected,actual)
-
-
#assertTrue(exper,msg=None)
-
def test_assert_true(self):
-
self.assertTrue(1)
-
self.assertTrue("hello world")
-
-
def test_assert_true_2(self):
-
d1=dict(a=1,b=2,c=3,d=4)
-
d2=dict(a=1,b=2)
-
self.assertTrue(set(d2.items()).issubset(set(d1.items())))
-
-
#assertGreater(a,b,msg=None):
-
def test_assert_greater(self):
-
self.assertGreater(2,1,msg="smaller")
-
-
-
#assertGreaterEqual(a,b,msg=None)
-
def test_assert_greater_equal(self):
-
self.assertGreaterEqual(2,2)
-
-
-
#assertIn(member,container,msg=None)
-
def test_assert_in(self):
-
self.assertIn(1,[1,2,3,4,5])
-
-
#assertIs(expr1,expr2)
-
def test_assert_is(self):
-
self.assertIs("a","a")
-
-
#assertIsInstance(obj, class, msg=None):
-
def test_assert_is_instance(self):
-
self.assertIsInstance(1,numbers.Integral)
-
-
#assertNotIsInstance(obj,class, msg=None)
-
def test_assert_is_not_instance(self):
-
self.assertNotIsInstance(1,str)
-
-
#assertIsNone(obj, msg=None)
-
def test_assert_is_none(self):
-
self.assertIsNone(None)
-
-
#assertIsNot(expr1,expr2,msg=None)
-
def test_assert_is_not(self):
-
self.assertIsNot([],[])
-
-
#assertIsNotNone(obj,msg=None)
-
def test_assert_is_not_none(self):
-
self.assertIsNotNone(1)
-
-
#assertLess(a,b,msg=None)
-
def test_assert_less(self):
-
self.assertLess(1,2)
-
-
#assertLessEqual(a,b,msg=None)
-
def test_assert_less_equal(self):
-
self.assertLessEqual(1,2)
-
-
#This function not exits in python3
-
#assertItemsEqual(a,b,msg=None):
-
@unittest.skipIf(sys.version_info>(3,0),"not supported in this library version")
-
def test_assert_items_equal(self):
-
try:
-
self.assertItemsEqual([1,2,3],[3,2,1])
-
except AttributeError as e:
-
print(e)
-
-
#assertRaises(exeClass,callableObj, *args, **kwargs, msg=None)
-
def test_assert_raises(self):
-
self.assertRaises(IndexError,[].pop,0)
-
-
-
@unittest.skip("showing class skipping")
-
class MySkippingTestCase(unittest.TestCase):
-
"""
-
def __init__(self):
-
unittest.TestCase.__init__(*args,**kwargs)
-
"""
-
-
def setUp(self):
-
self.a="5 is a good number"
-
self.b=self.test_run_3_post_condition()
-
#self.c=self.test_run_process_file()
-
#self.d=self.test_run_process2()
-
pass
-
-
-
def test_not_run(self):
-
pass
-
"""
-
def skipUnlessHasattr(obj,attr):
-
if hasattr(obj,attr):
-
return labda func: func
-
return unittest.skip("{!r} does not have {!r}".format(obj, attr))
-
"""
-
-
def test_run_3_post_condition(self):
-
for i in range(1,100):
-
if i==5:
-
return "{} is a good number".format(i)
-
return "{} is a good number".format(5)
-
-
def test_run_1(self):
-
self.assertEqual(self.a,self.b)
-
-
def test_run_2(self):
-
self.assertTrue(self.a!=self.b,"thould be equal")
-
-
-
def test_rename_file(self):
-
#ret=os.rename("echo.sh","echo")
-
self.assertTrue(os.path.exists("echo.sh"))
-
-
def test_final(self):
-
self.assertEqual(self.test_run_process_1(),self.test_run_process_2())
-
-
import difflib
-
class MyTest(unittest.TestCase):
-
"""
-
def __init__(self):
-
super().__init__()
-
"""
-
-
def setUp(self):
-
pass
-
-
def test_run_process_1(self):
-
proc=subprocess.Popen("./echo.sh",stdout=subprocess.PIPE,stderr=subprocess.PIPE)
-
sout,serr=proc.communicate()
-
stdout=sout.decode().strip()
-
with open("time_begin.txt","wt") as tb:
-
tb.write(stdout)
-
return True
-
-
def test_run_process_2(self):
-
time.sleep(5)
-
proc=subprocess.Popen("./echo.sh",stdout=subprocess.PIPE,stderr=subprocess.PIPE)
-
sout,serr=proc.communicate()
-
stdout=sout.decode().strip()
-
with open("time_finish.txt","wt") as tb:
-
tb.write(stdout)
-
return True
-
-
#def test_s1(self):
-
# self.assertEqual(self.test_run_process_1(),self.test_run_process_2())
-
-
def test_s2(self):
-
with open("time_begin.txt") as f1:
-
fromlines=f1.readlines()
-
with open("time_finish.txt") as f2:
-
tolines=f2.readlines()
-
diff=difflib.unified_diff(fromlines,tolines)
-
sys.stdout.writelines(diff)
-
-
def test_s3(self):
-
self.assertFalse(filecmp.cmp("time_begin.txt","time_finish.txt"))
-
-
if __name__=="__main__":
-
#unittest.main() -verbosity 2
-
suite=unittest.TestLoader().loadTestsFromTestCase(MyTest)
-
unittest.TextTestRunner(verbosity=2).run(suite)
以前C语言还要先test 传进去的参数呢,python 这些测试框架整的够复杂的,方法真多。真的是有用还是语法糖太多啊,哎。搞不清楚。
阅读(1472) | 评论(0) | 转发(0) |