Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为具有屈服的函数编写单元测试_Python_Unit Testing_Generator - Fatal编程技术网

Python 为具有屈服的函数编写单元测试

Python 为具有屈服的函数编写单元测试,python,unit-testing,generator,Python,Unit Testing,Generator,我正在尝试为使用生成器的函数编写单元测试。下面是我的代码: def extract_data(body): for i in body: a = re.sub('<[^<]+?>', '', str(i)) b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a)) c = re.sub('key', '', str(b)) d = re.sub('\xc2

我正在尝试为使用生成器的函数编写单元测试。下面是我的代码:

def extract_data(body):
    for i in body:
        a = re.sub('<[^<]+?>', '', str(i))
        b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a))
        c = re.sub('key', '', str(b))
        d = re.sub('\xc2', ' ', str(c))
        e = re.sub('\xa0', '', str(d))
        yield e
我的单元测试代码:

    def test_extract_data(self):
        sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
        expected_res = 'This Test Passes'
        res = extract_data(sample_input)

        self.assertEqual(expected_res, res)

如果extract_data函数使用return而不是yield,则此测试将无问题通过。如何为生成器编写测试?

您的代码稍微修改为不需要unittest:

import re

def extract_data(body):
    for i in body:
        a = re.sub('<[^<]+?>', '', str(i))
        b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a))
        c = re.sub('key', '', str(b))
        d = re.sub('\xc2', ' ', str(c))
        e = re.sub('\xa0', '', str(d))
        yield e

def test_extract_data():
    sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
    expected_res = 'This Test Passes'
    res = extract_data(sample_input)
    return expected_res == res

print(test_extract_data())
这是真的

举例来说,在


您的代码稍微修改为不需要unittest:

import re

def extract_data(body):
    for i in body:
        a = re.sub('<[^<]+?>', '', str(i))
        b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a))
        c = re.sub('key', '', str(b))
        d = re.sub('\xc2', ' ', str(c))
        e = re.sub('\xa0', '', str(d))
        yield e

def test_extract_data():
    sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
    expected_res = 'This Test Passes'
    res = extract_data(sample_input)
    return expected_res == res

print(test_extract_data())
这是真的

举例来说,在


我知道我需要做什么。我需要把res列成一个列表。就这样。比我想象的简单多了。这就是它现在的样子:

class TestScrapePage(unittest.TestCase):

    def test_extract_data(self):
        sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
        expected_res = ['This Test Passes']
        res = list(extract_data(sample_input))

    self.assertEqual(expected_res, res)

if __name__ == '__main__':
    unittest.main()

我知道我需要做什么。我需要把res列成一个列表。就这样。比我想象的简单多了。这就是它现在的样子:

class TestScrapePage(unittest.TestCase):

    def test_extract_data(self):
        sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
        expected_res = ['This Test Passes']
        res = list(extract_data(sample_input))

    self.assertEqual(expected_res, res)

if __name__ == '__main__':
    unittest.main()

除了你要问的问题之外,你真的不应该用正则表达式来解析HTML,这在一般情况下是不可能做到的。有一个非常好的库可以帮你做到这一点,我强烈建议你使用它:@Lefty你的问题得到回答了吗?@Kyle_Falconer,谢谢你的提示。我不喜欢使用正则表达式来解析HTML,但是在这种情况下,它工作得最好。除了你要问的问题,你真的不应该使用正则表达式来解析HTML,这在一般情况下是不可能的。有一个非常好的库可以帮你做到这一点,我强烈建议你使用它:@Lefty你的问题得到回答了吗?@Kyle_Falconer,谢谢你的提示。我不喜欢使用正则表达式来解析HTML,但是在这种特殊情况下,它工作得最好。我相信你的意思是,在你的第二段代码中,nextexpected_res==res。@michael_j_ward谢谢你的帮助!几乎expected\u res是一个str,因此下一个是res-:-我相信你的意思是,在你的第二个片段中,nextexpected\u res==res。@michael\u j\u ward谢谢你的关注!几乎expected_res是str,因此下一个是res-:-
class TestScrapePage(unittest.TestCase):

    def test_extract_data(self):
        sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']
        expected_res = ['This Test Passes']
        res = list(extract_data(sample_input))

    self.assertEqual(expected_res, res)

if __name__ == '__main__':
    unittest.main()