作者:清洁剂没看见家门口_200 | 来源:互联网 | 2022-12-13 09:46
ImtryingtogetintoUnittestingfortheobviouspositivesitintroduces,andImtryingtowrite
I'm trying to get into Unit testing for the obvious positives it introduces, and I'm trying to write a Unit test for a class I wrote the other day. (I know this is the opposite to TDD, please bear with me)
我正试图进行单元测试,看看它引入的明显积极因素,我正在尝试为前几天写的一个类写一个单元测试。 (我知道这与TDD相反,请耐心等待)
My class, Image
, is used in conjunction with some others for image manipulation.
我的课程,Image,与其他一些人一起用于图像处理。
Image
essentially wraps a GD image resource and stores data along with it. For example, an instance of Image
will always contain it's current state, i.e. its new width/height if resized, the original image data, etc.
Image实际上包装了GD图像资源并随之存储数据。例如,Image的一个实例将始终包含它的当前状态,即调整大小时的新宽度/高度,原始图像数据等。
The Image
class also contains methods for,
Image类还包含方法,
- Creating itself from a file, string data, or URL, e.g.
$image->loadFromPath()
从文件,字符串数据或URL创建自己,例如$图像 - > loadFromPath()
- Creating a new GD image resource from the properties of the current
Image
instance, e.g. for image resizing to maintain background transparency, etc.
从当前Image实例的属性创建新的GD图像资源,例如用于调整图像大小以保持背景透明度等
- Cloning the GD image resource for use in the manipulation classes
克隆GD图像资源以在操作类中使用
What I'm struggling with is how to Unit test this class properly with PHPUnit. I've done some reading and I have a few conflicting ideas on how to approach it and I don't know what's right. Do I,
我正在努力的是如何使用PHPUnit正确地测试这个类。我已经完成了一些阅读,我对如何处理它有一些相互矛盾的想法,我不知道什么是正确的。我,我
- Write a test for each method of the class. I read somewhere that I should test each and every method. However, some of the methods run others (rightly so may I add), so you then have a chain of dependency. But I also read that each Unit test should be independent from the other. So what do I do if this is the case?
为每个类的方法编写一个测试。我在某处读到了我应该测试的每一种方法。但是,有些方法会运行其他方法(正确地说,我可以添加),因此您将拥有一系列依赖关系。但我也读到每个单元测试应该独立于另一个。那么如果是这样的话我该怎么办?
- Write each test as a usage route of the class. I also read somewhere that each test should instead represent 1 path/usage route you can take with the class. Therefore if you cover every usage, you'll ultimately get complete code coverage.
将每个测试写为类的使用路径。我还在某处读到,每个测试应该代表您可以在课程中使用的1个路径/使用路径。因此,如果您涵盖所有用途,您最终将获得完整的代码覆盖率。
So, which of these is correct, if any?
那么,哪些是正确的,如果有的话?
3 个解决方案
9
Unit tests should be written to evaluate the public interface of a class. Your test case should use the class as you intend it to be used in your program. The idea here is to test the behavior (either expected, unexpected, or edge conditions) of the class.
应编写单元测试来评估类的公共接口。您的测试用例应该使用该类,因为您打算在程序中使用它。这里的想法是测试类的行为(预期,意外或边缘条件)。
Both ideas you posted are correct. In theory, you should have enough test cases (routes through your code) that all your methods in the class are run.
您发布的两个想法都是正确的。理论上,您应该有足够的测试用例(通过代码的路由),以便运行类中的所有方法。
As was mentioned, 100% test coverage is a nice goal, but not always realistic.
如前所述,100%的测试覆盖率是一个很好的目标,但并不总是现实的。
Also, in the case of GD, be careful about writing unit tests that test GD's functionality (it's already been tested, you don't need to waste time testing it again). I would read up on using PHPUnit's mocks and stubs (and mocking the filesystem) in the PHPUnit manual.
此外,在GD的情况下,要小心编写测试GD功能的单元测试(它已经过测试,你不需要浪费时间再次测试它)。我会在PHPUnit手册中阅读使用PHPUnit的模拟和存根(以及模拟文件系统)。
Here's what an example test might look like:
以下是示例测试的样子:
public function testImageIsResized()
{
$image = new Image();
$image->loadFromPath('some/path');
$image->resize(200, 300);
$this->assertEquals(200, $image->getWidth());
$this->assertEquals(300, $image->getHeight());
}
Now, depending on the expected behavior of the image class, this test might pass without issue, or it might fail because it was expecting the new dimensions to be proportionally constrained to the original image dimensions. But we did not explicitly call the internal method that checks for that constraint in the test itself.
现在,根据图像类的预期行为,这个测试可能通过没有问题,或者因为它期待新的尺寸成比例地限制原始图像尺寸也可能会失败。但是我们没有显式调用在测试本身中检查该约束的内部方法。