单元测试属性错误。 'mymodule'没有属性'myclass'

时间:2019-06-12 08:24:31

标签: python module python-unittest

我正在为我编写的课程编写单元测试课程。

我具有以下结构。首先是我的模块。

NewsStream.py

from enum import Enum
import xml.etree.ElementTree as ET
import numpy as np # linear algebra
import pandas as pd # data processing
import json
import urllib.request

# XML parser

class XMLParser:
    BASE_PATH = "../input" # TODO: substitute this with an online streaming.

    def __init__(self, file_path = ''):
        """
            Initializes the XMLParser class instance.
            :param file_path: Path to input xml file containing all the data.
            """
        self.file_path = file_path


    def xml_to_pandas_df(self, columns = []):
        """
            Using the standard xml python library, we parse the data xml file and convert the xml data to a pandas data frame.
            :return: A pandas data frame instance containing all the data.
            """

        xml = objectify.parse(self.file_path)
        root = xml.getroot()

        data=[]
        for i in range(len(root.getchildren())):
            data.append([child.text for child in root.getchildren()[i].getchildren()])

        df = pd.DataFrame(data)
        df.columns = columns

        return df

    def url_to_pandas_df(self, url, set_index = False, index_name = None):
        """
            Using urllib python library, we download the data from a url and convert them into a pandas dataframe.
            :return: a pandas dataframe instance containing all the data
            """

        f = urllib.request.urlopen(url)
        myfile = f.read()
        data = json.loads(myfile.decode('utf-8'))
        df = pd.DataFrame(data)

        if set_index:
            df = df.set_index(index_name)

        return df

因此,我的单元测试课

LKCluster-unittest.py

import NewsStream
import unittest

BASE_PATH = './TestInput/'

class test_XMLparser(unittest.TestCase):
    """ Test class for functions of XMLparser. """


    def test_parser_xml_empty(self):
        """ Test xml parser in the case of an empty xml. """

        file_path = BASE_PATH + 'empty.xml'
        parser = NewsStream.XMLparser(file_path)
        df = parser.xml_to_pandas_df()
        actual = df.empty
        expected = True
        self.assertEqual(actual, expected)

    def test_parser_xml_50(self):
        """ Test xml parser in the case of a fifty-record xml. """

        file_path = BASE_PATH + 'test50.xml'
        columns = ['CategoryCode', 'DateTime', 'ID', 'SourceCode', 'Text', 'Title']
        parser = NewsStream.XMLparser(file_path)
        df = parser.xml_to_pandas_df(columns = columns)
        actual = len(df)
        expected = 50
        self.assertEqual(actual, expected)


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

使用shell解释时,会出现以下错误

~:$ python3 LKCluster-unittest.py 
EE
======================================================================
ERROR: test_parser_xml_50 (__main__.test_XMLparser)
Test xml parser in the case of a fifty-record xml.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "LKCluster-unittest.py", line 32, in test_parser_xml_50
    parser = NewsStream.XMLparser(file_path)
AttributeError: module 'NewsStream' has no attribute 'XMLparser'

======================================================================
ERROR: test_parser_xml_empty (__main__.test_XMLparser)
Test xml parser in the case of an empty xml.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "LKCluster-unittest.py", line 21, in test_parser_xml_empty
    parser = NewsStream.XMLparser(file_path)
AttributeError: module 'NewsStream' has no attribute 'XMLparser'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=2)

为什么看不到类函数? 当我在脚本中使用该类时,它可以完美运行。

p.s.:python版本是3.7,我在Mac OSX和Ubuntu上都运行此

0 个答案:

没有答案