zoukankan      html  css  js  c++  java
  • python 测试代码

    1、使用print()打印

    测试代码最简单的就是添加一些print()语句。然而产品开发中,需要记住自己添加的所有print()语句并在最后删除,很容易出现失误。

    2、使用pylint、pyflakes和pep8检查代码

    这些包可以检查代码错误和代码风格问题。

    • pip install pylint
    • pip install pyflakes
    style1.py:
    a=1
    b=2
    print(a)
    prnt(b)
    print(c)
    ------------------------------
    $ pylint style1.py

    输出E带头为Error。

    • pip install pep8
    $ pep8 style1.py

    3、使用unittest测试

    用于测试代码程序逻辑。

    • 确定输入对应的期望输出(断言)
    • 传入需要测试的函数,检查返回值和期望输出是否相同,可以使用assert检查。
    test_cap.py:
    ---------------------------
    import unittest
    from string import capwords
    def just_do_it(text):
        return text.capitalize()#换成text.title()、capwords(text)
    class TestCap(unittest.TestCase):
        def setUp(self): #在每个测试方法前执行
            pass
        def tearDown(self):#在每个测试方法执行后执行
            pass
        def test_one_word(self):
            text='duck'
            result=just_do_it(text)
            self.assertEqual(result,'Duck')
        def test_multiple_words(self):
            text='a veritable flock of ducks'
            self.assertEqual(result,'A veritable Flock Of Ducks')
        def test_words_with_apostrophes(self):
            text="I'm fresh out of ideas"
            result=just_do_it(text)
            self.assertEquals(result,"I'm Fresh Out Of Ideas")
        def test_words_with_quotes(self):
            text=""You're despicable," said Daffy Duck"
            result=just_do_it(text)
            self.assertEqual(result,""You're Despicable," Said Daffy Duck")
    if __name__=='__main__':
        unittest.main()

    4、使用doctest测试

    可以把测试写到文档字符串中,也可以起到文档作用。

    cap.py:
    -------------------------------
    def just_do_it(text):
        """
        >>>just_do_it('duck')
        'Duck'
        >>>just_do_it('a veritable flock of ducks')
        'A Veritable Flock Of Ducks'
        >>>just_do_it("I'm fresh out of ideas")
        "I'm Fresh Out Of Ideas"
        """
        from string import capwords
        return capwords(text)
    
    if __name__=='__main__':
        import doctest
        doctest.testmod()
    ----------------------------------------------
    $ python cap.py -v

    5、使用nose测试

    和unittest类似,但不需要创建一个包含测试方法的类,任何名称中带有test的函数都会被执行。

    test_cap.py:
    ---------------------------
    from nose.tools import eq_
    from string import capwords
    def just_do_it(text):
        return text.capitalize()#换成text.title()、capwords(text)
    def test_one_word(self):
        text='duck'
        result=just_do_it(text)
        eq_(result,'Duck')
    def test_multiple_words(self):
        text='a veritable flock of ducks'
        eq_(result,'A veritable Flock Of Ducks')
    def test_words_with_apostrophes(self):
        text="I'm fresh out of ideas"
        result=just_do_it(text)
        eq_(result,"I'm Fresh Out Of Ideas")
    def test_words_with_quotes(self):
        text=""You're despicable," said Daffy Duck"
        result=just_do_it(text)
        eq_(result,""You're Despicable," Said Daffy Duck")
    ----------------------------------------------------
    $ nosetests test_op.py

    6、其他测试框架

    • tox
    • py.test
  • 相关阅读:
    微信小程序の模板
    微信小程序の条件渲染
    微信小程序のwxml列表渲染
    769. Max Chunks To Make Sorted
    766. Toeplitz Matrix
    747. Largest Number At Least Twice of Others
    746. Min Cost Climbing Stairs
    729. My Calendar I
    724. Find Pivot Index
    718. Maximum Length of Repeated Subarray
  • 原文地址:https://www.cnblogs.com/ivan-count/p/10515713.html
Copyright © 2011-2022 走看看