简体   繁体   English

Python Configparser在单元测试下找不到部分?

[英]Python Configparser cannot find section under Unittest?

So I have to test a Config file. 所以我必须测试一个配置文件。 In this Config file a ConfigParser instance will be initialized then load a config file. 在此配置文件中,将初始化ConfigParser实例,然后加载配置文件。

In the Unittest I imported this parser file then tried to read a node of this ConfigParse instance. 在单元测试中,我导入了此解析器文件,然后尝试读取此ConfigParse实例的节点。 But then it raises a error that it cannot find the section. 但是随后引发了一个错误,即找不到该节。

Howcome? 怎么会? Is there a way to fix it? 有办法解决吗?

edited: So the Unittest just imported the Config file. 编辑:所以单元测试只是导入配置文件。 The name of the config file is called config. 配置文件的名称称为config。 the instance is called p_config In a test case the instance will be called like: 该实例称为p_config。在一个测试案例中,该实例的名称如下:

config.p_config.get('section1','a')

The Config file look pretty standard. 配置文件看起来很标准。

import ConfigParser
p_config = ConfigParser.SafeConfigParser()
p_config.read("xxx.cfg")

so my config file looks pretty much like a normal windows config: 所以我的配置文件看起来很像普通的Windows配置:

[section1]
a = 1
b = 2
[section2]
c = 2
d = 4

The error it raised just says it cannot find section: 它引发的错误只是表明找不到部分:

ConfigParser.NoSectionError: No section: 'section1'

The content of the UnitTest: UnitTest的内容:

class TestConfigFile(TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass


    def test_example(self):
        print global_path_config.get('section1', 'a')

It sounds like you are setting test config object up without actually providing a configuration file for your test to read. 听起来您正在设置测试配置对象,而没有实际提供配置文件供测试读取。

I assume you are using the python unittest module. 我假设您正在使用python unittest模块。

How does your command line differ from running your project and running your tests? 您的命令行与运行项目和运行测试有何不同? The command line may be an issue. 命令行可能是个问题。

For example, if you use python /path/to/file.py <--options> config.ini to launch your program and python -m unittest /path/to/config/unittest -v to launch your unit tests, then your configuration file is never being read. 例如,如果您使用python /path/to/file.py <--options> config.ini启动程序,并使用python -m unittest /path/to/config/unittest -v启动单元测试,则您的配置文件永远不会被读取。

In setUp(self): you would need to initialize the object with the config file location. setUp(self):您将需要使用配置文件位置来初始化对象。 I prefer to write a short, fake config file in setUp so I can easily remove it in tearDown and know exactly how it is related to my tests without polluting my working configuration file. 我喜欢写在很短的,假的配置文件setUp ,所以我可以很容易去除tearDown ,知道它究竟是如何与我的测试,不污染我的工作配置文件。

I advice you to give up ConfigParser if you can. 如果可以的话,我建议您放弃ConfigParser。 Use a json file for your config 使用json文件进行配置

import json
from collections import OrderedDict

def read_config(file_json):
    with open(file_json, 'r') as filename:
        config = filename.read()
    return json.loads(config, object_pairs_hook=OrderedDict)

def write_config(config, file_json):
     data = json.dumps(config, indent=4)
     with open(file_json, 'w') as filename:
         filename.write(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM