关于PHPUnit的一些疑问
看了PHPunit的断言之后 我有点疑问 PHPUnit有什么意义吗? 我大可以var_dump 等一系列的调试方法来输出变量的类型/数值/长度,有需要为了判断这东西是什么 而且去写一些断言什么的?
可能我经验尚浅不懂PHPUnit具体深层次的运用。。请哪位大神解惑一下
回复内容:
看了PHPunit的断言之后 我有点疑问 PHPUnit有什么意义吗? 我大可以var_dump 等一系列的调试方法来输出变量的类型/数值/长度,有需要为了判断这东西是什么 而且去写一些断言什么的?
可能我经验尚浅不懂PHPUnit具体深层次的运用。。请哪位大神解惑一下
var_dump是临时性质的,phpunit是持久可反复的
你总不可能次次都到处加var_dump来做全面测试吧?
别忘记还有个mock, 搭配起来使用.
你觉得可以用var_dump替代, 可能业务逻辑太简单了?
我设计的类都得依赖于外部的类, 类的方法还得需要其他的内部方法协作.
数据输入的不同会有不同的结果, 我不能每次都在浏览器上填10几个数据来做测试.
考虑到这10几个输入, 我设计这个类时, 就会把数据分组, 一个小方法一组数据.
对小方法做单元测试时, 我只要对其依赖做一些假设(假设依赖没有bug)和传入期望参数(极端的和正常的), 再对其返回的结果进行断言即可.
最后, 我再对这个类对外开放的接口做单元测试, 假设所有小方法的返回数据并断言这个接口返回的数据.
可以看出, 单元测试还是有助于代码结构的设计.
给个比较简单的实例https://github.com/Zizaco/confide/blob/master/tests/Confide/CacheLoginThrottleServiceTest.php
在对repository这种低级类做单元测试时,我只用assert, 直接数据库操作..
在高级类里, 反正咱都对repository进行了测试, 就不操心数据库问题了, 直接mock repository类.
你这个问题嘛, 我觉得问错方向了, 应该是"为什么单元测试", "why unit testing", 能google到很多答案的.
我刚玩unit testing没多久, 到现在还分不清stub, spy, dummy, mock, double...
所以我上面写的东西,你就随便看看啊, 别太当真... 但是,有错误的话,欢迎指正.
同学,这个问题如果你还在那PHPUnit 和 var_dump 作比较的话,证明你没理解单元测试的优势!先去找找单元测试的资料理解下就知道了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
