热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHPUnit测试问题-如何对我的班级进行单元测试-PHPUnitTestQuestion-HowtoUnittestmyclass

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正确地测试这个类。我已经完成了一些阅读,我对如何处理它有一些相互矛盾的想法,我不知道什么是正确的。我,我

  1. 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?
  2. 为每个类的方法编写一个测试。我在某处读到了我应该测试的每一种方法。但是,有些方法会运行其他方法(正确地说,我可以添加),因此您将拥有一系列依赖关系。但我也读到每个单元测试应该独立于另一个。那么如果是这样的话我该怎么办?

  3. 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.
  4. 将每个测试写为类的使用路径。我还在某处读到,每个测试应该代表您可以在课程中使用的1个路径/使用路径。因此,如果您涵盖所有用途,您最终将获得完整的代码覆盖率。

So, which of these is correct, if any?

那么,哪些是正确的,如果有的话?

3 个解决方案

#1


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.

现在,根据图像类的预期行为,这个测试可能通过没有问题,或者因为它期待新的尺寸成比例地限制原始图像尺寸也可能会失败。但是我们没有显式调用在测试本身中检查该约束的内部方法。

#2


5  

You can use the covers annotation to specify if a test covers multiple methods. So if one of your methods calls another method, you can simply add the annotation to the test's docblock and it will be added to your code coverage statistics, e.g.

您可以使用封面注释来指定测试是否涵盖多种方法。因此,如果您的某个方法调用另一个方法,您只需将注释添加到测试的docblock中,它就会被添加到您的代码覆盖率统计信息中,例如:

/**
 * @test
 * @covers MyClass::something()
 * @covers MyClass::_somethingElse()
 */
public function somethingWorksAsExpected()
{
    $this->assertSame($expected, $this->testObject->something());
}

For personal projects, 100% Code coverage is fine. However, I've seen talks at conferences where 100% are doubted to be necessary. Despite all the benefits, tests take time to write and in a budgeted project it might be sufficient to just test 80/20 and leave out uncritical low priority features of your app.

对于个人项目,100%的代码覆盖率是好的。但是,我在会议上看到过100%被怀疑是必要的会议。尽管有各种好处,但测试需要时间来编写,在预算项目中,仅测试80/20并省略应用程序的非关键性低优先级功能就足够了。

As for how to test your class, have a look at the chapter on Behaviour Driven Development in the PHPUnit Manual. In your case, I'd test the functionality you described in your question.

至于如何测试你的类,请参阅PHPUnit手册中关于行为驱动开发的章节。在您的情况下,我会测试您在问题中描述的功能。

#3


0  

Stephen Melrose said:

斯蒂芬梅尔罗斯说:

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

但是,有些方法会运行其他方法(正确地说,我可以添加),因此您将拥有一系列依赖关系。但我也读到每个单元测试应该独立于另一个

Test independence isn't about not testing the same code twice, it's about whether the result (or lack of result) of one test affects the result of another. If your first test inserts some data and then fails before it can remove that data, your second test might get different results than expected. Ideally you should be able to run your tests in a random order, or run some and not others.

测试独立性不是关于不测试相同代码两次,而是关于一个测试的结果(或缺少结果)是否会影响另一个测试的结果。如果您的第一个测试插入一些数据然后在它可以删除该数据之前失败,那么您的第二个测试可能会得到与预期不同的结果。理想情况下,您应该能够以随机顺序运行测试,或运行一些而不是其他运行。


推荐阅读
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • vue使用
    关键词: ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • 鄂维南:从数学角度,理解机器学习的「黑魔法」,并应用于更广泛的科学问题...
    作者|Hertz来源|科学智能AISI北京时间2022年7月8日晚上22:30,鄂维南院士在2022年的国际数学家大会上作一小时大会报告(plenarytalk)。今 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 浏览器中的异常检测算法及其在深度学习中的应用
    本文介绍了在浏览器中进行异常检测的算法,包括统计学方法和机器学习方法,并探讨了异常检测在深度学习中的应用。异常检测在金融领域的信用卡欺诈、企业安全领域的非法入侵、IT运维中的设备维护时间点预测等方面具有广泛的应用。通过使用TensorFlow.js进行异常检测,可以实现对单变量和多变量异常的检测。统计学方法通过估计数据的分布概率来计算数据点的异常概率,而机器学习方法则通过训练数据来建立异常检测模型。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • testcafe的版本-1.7.0和1.7.1(最新)铬版本-78.0.3904.108运行环境 ... [详细]
author-avatar
清洁剂没看见家门口_200
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有