简体   繁体   English

如何使用phpunit此类进行测试,该类使用与数据库的连接

[英]How to test with phpunit this class, which uses connections to the database

I have read in another document that classes which have methods with connections to a database have to be tested by integration tests. 我读过另一份文档,其中包含与数据库连接的方法的类必须由集成测试进行测试。 I tried to find an example, but I didn't find any, only theory. 我试图找到一个例子,但是没有找到任何理论。 I've very little experience with testing. 我几乎没有测试经验。 Can you give me an example? 你能举个例子吗? a link?, something (instead of only theory, because I read a lot), with something like this?. 一个链接?,某些东西(而不是理论,因为我读了很多书),带有这样的东西?。 I think this is not an strange case. 我认为这并不奇怪。 If its possible with php and sql or similar. 如果可能的话,可以使用php和sql或类似的工具。

If you dont know about integration tests, how do you test this kinds of problems? 如果您不了解集成测试,那么如何测试这类问题?

class database{

public $server;
public $database;
public $password;
public $user;
public $descriptor;


function __construct(){
$this->server="localhost";
$this->database="mydb";
$this->user="stackoverflow";
$this->password="1234";

$this->descriptor=mysql_connect($this->server,$this->user,$this->password);
mysql_select_db($this->database,$this->descriptor); 
}


function make_request($query){
$response=mysql_query($query,$this->descriptor);
return $response;
}


function close_connection(){
mysql_close($this->descriptor);
}

function number_rows($result_query){
return mysql_num_rows($result_query);
}

}

Look into PHPUnit for unit testing. 查看PHPUnit进行单元测试。 You can then use mock objects to simulate the dependencies (DB calls) to return dummy data. 然后,您可以使用模拟对象来模拟依赖关系(DB调用)以返回虚拟数据。 Because you're trying to test some unit of logic and not that your database layer, it should be acceptable and give you a reasonable amount of confidence that your code is working. 因为您正在尝试测试某种逻辑单元而不是数据库层,所以它应该可以接受,并给您合理的信心,让您的代码可以正常工作。

Another option is to use integration tests, but this requires that you put a lot of effort into the setup and teardown of your tests. 另一种选择是使用集成测试,但这需要您在测试的设置和拆卸上付出很多努力。 You need to make sure any data required for your tests exists within your database. 您需要确保数据库中存在测试所需的任何数据。 It's common to use transactions for these types of tests which you can then rollback after your finished running those tests. 通常将事务用于这些类型的测试,然后在运行完这些测试后可以回滚。

Which code is it you're trying to test? 您要测试哪个代码? Is it the code that uses your database class? 它是使用您的database类的代码吗? If so, you don't need to introduce PDO, as your database class already provides an abstraction (well, it would be much safer if public $descriptor was private). 如果是这样,则您无需引入PDO,因为您的database类已经提供了抽象(嗯,如果public $descriptor是私有的,那将更加安全)。 So, you should start by mocking your database class and then testing the code that uses it. 因此,您应该从模拟database类开始,然后测试使用它的代码。 PHPUnit has a sophisticated mocking system . PHPUnit具有完善的模拟系统

Another thing you might want to be testing is this database class itself? 您可能要测试的另一件事是该database类本身? In that case you'll have to accept the MySQL database dependency, and the speed hit that gives you. 在这种情况下,您将不得不接受MySQL数据库的依赖关系,以及带来的快速打击。 Set up a completely dedicated MySQL database for that testing. 为该测试设置一个完全专用的MySQL数据库。 If you don't mind the speed hit, consider creating the database in a setUp() function and removing it after each test in a tearDown() function. 如果您不介意速度的提高,请考虑在setUp()函数中创建数据库,并在每次测试后在tearDown()函数中将其删除。

The third thing you might want to be testing is that your website (or whatever) works as a whole, and connects to the database, etc. This is functional testing (also called integration testing). 您可能要测试的第三件事是您的网站(或其他任何东西)可以整体运行,并连接到数据库等。这是功能测试(也称为集成测试)。 If your system offers a web services API, then you could use that to test it. 如果您的系统提供了Web服务API,则可以使用该API对其进行测试。 Otherwise you could use Selenium (still using PHPUnit) to test by remote control of a real browser. 否则,您可以使用Selenium(仍然使用PHPUnit)通过真实浏览器的远程控制进行测试。 If your tests will only read from the database, you could use your live system; 如果您的测试仅从数据库中读取,则可以使用实时系统;否则,您可以使用系统。 but it would be much more sensible to assume a write will happen at some point, so test against a test version of the website, with its own dedicated test database. 但是假设在某个时候进行写操作会更明智,因此请针对具有自己专用测试数据库的网站测试版本进行测试。

The advantage of functional testing is it high level, testing for all interactions between components, testing the system as the end user will use it. 功能测试的优点是高级别,测试组件之间的所有交互以及在最终用户使用它时测试系统。 The downside is when a test fails it only tells you something is wrong, not exactly which component is to blame. 不利的一面是,如果测试失败,它只会告诉您出了什么问题,而不是确切地归咎于哪个组件。 So we have unit tests to test each component in isolation. 因此,我们有单元测试来隔离测试每个组件。

I know you this is more text, less code, than you wanted! 我知道您这比您想要的更多文本,更少的代码! But you have actually asked quite a theoretical question! 但是您实际上已经提出了一个理论上的问题! Just try setting up some unit tests of this code, watch what problems occur over the first month of using them, and you'll understand why and where to use mocks. 只需尝试对此代码进行一些单元测试,观察在使用它们的第一个月中发生了什么问题,您就会了解为什么以及在何处使用模拟程序。

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

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