单元测试读取配置文件的类(Unit testing of a class reading a configuration file)

编程入门 行业动态 更新时间:2024-10-20 13:49:51
单元测试读取配置文件的类(Unit testing of a class reading a configuration file)

我有一个类“配置”,它有一个方法“getConfig”,它读取配置文件“config.ini”,我拥有所有应用程序配置(数据库凭证和主机,一些apis键,.....)

现在我有这个单元测试,用于测试配置文件中是否存在数据库条目,同时确认方法“getConfig”返回的数组具有“数据库”关键字:

function testConfigFileHasDatabaseEntry() { $configuration = Configuration::getInstance(); $arrConfig = $configuration->getConfig(); $this->assertArrayHasKey('database', $arrConfig); }

我还有另一个单元测试,确认“getConfig”返回一个数组类型的变量。

我的问题如下:就单元测试最佳实践而言,在这种情况下,此测试足以证实函数getConfig已经过良好测试,或者最好确认每个密钥都存在于配置文件中。 我认为确认所有条目都在配置文件中可能属于另一类测试,但我想确定。

让我知道这种情况下的最佳做法。

根据gcontrollez的回答,我意识到最好发布类源代码:

<?php /** * Loads the configuration file */ namespace Example\Lib; class Configuration { private static $instance; private $arrConfig; /** * Constructor: loads the config file into property arrConfig and dies if unable * to load config file * * @return void */ private function __construct() { $this->arrConfig = parse_ini_file("config/settings.ini", true); if ($this->arrConfig == false){ die("Cannot load configuration file"); } } /** * returns an instance of this singleton class * * @return Configuration */ public static function getInstance() { if (self::$instance == null){ self::$instance = new Configuration(); } return self::$instance; } /** * Getter for the property arrConfig * * @return array: */ public function getConfig() { return $this->arrConfig; } }

谢谢

I have a class "Configuration" that has a method "getConfig" that reads a configuration file "config.ini" where I have all the app configs (database credentials and host, some apis keys, .....)

For now I have this unit test that tests if the database entry exists in the config file and at the same time confirms that the array returned by the method "getConfig" has the key "database":

function testConfigFileHasDatabaseEntry() { $configuration = Configuration::getInstance(); $arrConfig = $configuration->getConfig(); $this->assertArrayHasKey('database', $arrConfig); }

I have also another unit test that confirms that "getConfig" returns a variable of type array.

My question is the following: In terms of unit testing best practices, in this case is this test enough to confirm that the function getConfig is well tested or it is better to confirm that every key exists in the config file. I think confirming that all entries are in the config file maybe falls under another category of testing, but I want to be sure.

Let me know what is the best practice in this case.

Base on the answer of gcontrollez I realized it is better to post the class source code:

<?php /** * Loads the configuration file */ namespace Example\Lib; class Configuration { private static $instance; private $arrConfig; /** * Constructor: loads the config file into property arrConfig and dies if unable * to load config file * * @return void */ private function __construct() { $this->arrConfig = parse_ini_file("config/settings.ini", true); if ($this->arrConfig == false){ die("Cannot load configuration file"); } } /** * returns an instance of this singleton class * * @return Configuration */ public static function getInstance() { if (self::$instance == null){ self::$instance = new Configuration(); } return self::$instance; } /** * Getter for the property arrConfig * * @return array: */ public function getConfig() { return $this->arrConfig; } }

Thanks

最满意答案

你不是单元测试getConfig方法。 您只是检查其结果是否包含某些数据。 这是完全有效的,可以是有用的,但不是单元测试。

如果你想单元测试getConfig方法,你应该检查它的行为与每个可能的config.ini文件。 我想你正在使用getConfig的parse_ini_file函数。 所以为了单元测试getConfig方法,你应该检查你认为可能发生的所有可能的情况:

- testConfigFileIsLoaded - testExceptionThrownIfConfigFileFailsToLoad

parse_ini_file返回一个数组或false,所以这些是你应该检查的情况。

为此,您需要能够更改要使用的.ini文件。 如果它在Configuration类中被编码,你将无法做到这一点。

You are not unit testing the getConfig method. You are just checking that its result contains certain data. This is perfectly valid and can be useful but is not a unit test.

If you wanted to unit test the getConfig method, you should check it's behaviour with each possible config.ini file. I suppose you are using the parse_ini_file function inside getConfig. So in order to unit test the getConfig method you should check all the possible cases you think that can happen:

- testConfigFileIsLoaded - testExceptionThrownIfConfigFileFailsToLoad

The parse_ini_file returns an array or false so those are the cases that you should check.

For that you need to be able to change the .ini file to be used. If it's harcoded inside the Configuration class you wont be able to do that.

更多推荐

本文发布于:2023-07-18 04:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   单元测试   Unit   testing   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!