国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Pythonunittest

2019-11-14 17:35:35
字體:
來源:轉載
供稿:網友
  1. unittest module包含了編寫運行unittest的功能,自定義的test class都要集成unitest.TestCase類,test method要以test開頭,運行順序根據test method的名字排序,特殊方法:
    • setup():每個測試函數運行前運行
    • teardown():每個測試函數運行完后執行
    • setUpClass():必須使用@classmethod 裝飾器,所有test運行前運行一次
    • tearDownClass():必須使用@classmethod裝飾器,所有test運行完后運行一次
  2. 示例代碼:
    #文件名runtest.py
    import randomimport unittestclass TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = list(range(10)) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, list(range(10))) # should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2,3)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq)if __name__ == '__main__': unittest.main()
    運行方式:在命令行直接運行這個runtest.py
  3. 可以使用unitest.skip裝飾器族跳過test method或者test class,這些裝飾器包括:
    • @unittest.skip(reason):無條件跳過測試,reason描述為什么跳過測試
    • @unittest.skipif(conditition,reason):condititon為true時跳過測試
    • @unittest.skipunless(condition,reason):condition不是true時跳過測試
    • 可以自定義skip decorator
      #這是一個自定義的skip decorratordef skipUnlessHasattr(obj, attr):    if hasattr(obj, attr):        return lambda func: func    return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))
    • skip decorator示例代碼:
      class MyTestCase(unittest.TestCase):    @unittest.skip("demonstrating skipping")    def test_nothing(self):        self.fail("shouldn't happen")    @unittest.skipIf(mylib.__version__ < (1, 3),                     "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@unittest.skip("showing class skipping")class MySkippedTestCase(unittest.TestCase):    def test_not_run(self):        pass
  4. expected failure:使用@unittest.expectedFailure裝飾器,如果test失敗了,這個test不計入失敗的case數目

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辉南县| 宝山区| 大同县| 丰县| 杭州市| 响水县| 南靖县| 龙口市| 建德市| 巴南区| 广饶县| 名山县| 五常市| 阜宁县| 措勤县| 积石山| 永安市| 鹤峰县| 临澧县| 县级市| 镇宁| 泗水县| 伽师县| 曲松县| 庐江县| 射阳县| 墨脱县| 莱州市| 梨树县| 青河县| 苍梧县| 克山县| 上林县| 任丘市| 炎陵县| 墨脱县| 兴国县| 嘉兴市| 永泰县| 牡丹江市| 赤壁市|