前言: 真正写php代码也有3年时间了,勉强算是一个php程序员, 但是,心底却一直没有底气。 都说测试驱动开发,可我连程序开发中什么是单元测试?这种基本的程序员的素养都

还不是很清楚,痛定思痛,决定这些基本的知识技能还是要有所了解和掌握。要不然,一直用着别人现成的框架,写着一些简单的业务逻辑代码,宝宝心里其实是慌的 :).

这篇文章不错:https://www.sitepoint.com/tutorial-introduction-to-unit-testing-in-php-with-phpunit/

在 stack-overflow上面有人这样回答的:

url: http://stackoverflow.com/questions/282150/how-do-i-write-unit-tests-in-php

Question: How do I write unit tests in PHP? [closed]

I've read everywhere about how great they are, but for some reason I can't seem to figure out how exactly I'm supposed to test something. Could someone perhaps post a piece of example code and how they would test it? If it's not too much trouble :)

Answer:

There is a 3rd "framework", which is by far easier to learn - even easier than SimpleTest, it's called phpt.

A primer can be found here: http://qa.php.net/write-test.php

Edit: Just saw your request for sample code.

Let's assume you have the following function in a file called lib.php:

<?php
function foo($bar)
{
return $bar;
}
?>

Really simple and straight forward, the parameter you pass in, is returned. So let's look at a test for this function, we'll call the test file foo.phpt:

--TEST--
foo() function - A basic test to see if it works. :)
--FILE--
<?php
include 'lib.php'; // might need to adjust path if not in the same dir
$bar = 'Hello World';
var_dump(foo($bar));
?>
--EXPECT--
string(11) "Hello World"

In a nutshell, we provide the parameter $bar with value "Hello World" and we var_dump() the response of the function call to foo().

To run this test, use: pear run-test path/to/foo.phpt

This requires a working install of PEAR on your system, which is pretty common in most circumstances. If you need to install it, I recommend to install the latest version available. In case you need help to set it up, feel free to ask (but provide OS, etc.).

another answer:There are two frameworks you can use for unit testing. Simpletest and PHPUnit, which I prefer. Read the tutorials on how to write and run tests on the homepage of PHPUnit. It is quite easy and well described.

这里是phpunit的官方入门的文档:https://phpunit.de/getting-started.html

另外, simpleTest的官方文档:http://www.simpletest.org/

好吧,下面开始学习 phpunit 吧,从今天开始,慢慢了解吧,加油!

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

补充:这里有另外一篇介绍文章:

http://code.tutsplus.com/articles/the-beginners-guide-to-unit-testing-what-is-unit-testing--wp-25728

Depending on your background, you may or may not have heard of unit testing, test-driven development, behavior-driven development, or some other type of testing methodology. Often times, these methodologies are applied in the context of larger software systems or applications and less in the context of WordPress-based projects (though it is getting better!)

Frankly, the development community is a bit divided on automated software testing – you have some people who think you should have tests for 100% of all of your code, some believe that 80% is sufficient, some 50%, and some are content with 20% or so. Whatever the case may be, this article is not about arguing a case for the level of tests you should have in your project nor is it taking a position on general software testing.

Instead, we're going to take a look at what's required to get up and running with unit testing your WordPress development projects. We're going to be approaching this series from the perspective of an absolute beginner so that we can understand the benefits of unit testing and how to configure our environment to support unit testing libraries so that we can begin to do this in our future work. Finally, all of this will be done by building and testing a simple, testable plugin from the ground up.

What Is Unit Testing?

Before we get started setting up our environment and writing any code, let's define exactly what unit testing is, why it's worth doing, and how to get started in incorporating it in our projects.

At a high-level, unit testing refers to the practice of testing certain functions and areas – or units – of our code. This gives us the ability to verify that our functions work as expected. That is to say that for any function and given a set of inputs, we can determine if the function is returning the proper values and will gracefully handle failures during the course of execution should invalid input be provided.

Ultimately, this helps us to identify failures in our algorithms and/or logic to help improve the quality of the code that composes a certain function. As you begin to write more and more tests, you end up creating a suite of tests that you can run at any time during development to continually verify the quality of your work.

A second advantage to approaching development from a unit testing perspective is that you'll likely be writing code that is easy to test. Since unit testing requires that your code be easily testable, it means that your code must support this particular type of evaluation. As such, you're more likely to have a higher number of smaller, more focused functions that provide a single operation on a set of data rather than large functions performing a number of different operations.

A third advantage for writing solid unit tests and well-tested code is that you can prevent future changes from breaking functionality. Since you're testing your code as you introduce your functionality, you're going to begin developing a suite of test cases that can be run each time you work on your logic. When a failure happens, you know that you have something to address.

Of course, this comes at the expense of investing time to write a suite of tests early in development, but as the project grows you can simply run the tests that you've developed to ensure that existing functionality isn't broken when new functionality is introduced.

.........

php单元测试到底是什么东西呢?的更多相关文章

  1. CPU到底是什么东西?它为什么能够执行数学运算?

    CPU到底是什么东西?它为什么能够执行数学运算? 本文地址http://yangjianyong.cn/?p=20转载无需经过作者本人授权 简单的物理电路 先来看一张初中学过的物理电路图: 从图中我们 ...

  2. 关于RundownProtect到底是什么东西

    RundownProtect这个字段相信只要是读过WRK源码的都会看过这个东西,这个字段在进程和线程的结构中都存在.最典型的例子就是对进程要进行什么操作的时候会先引用这个字段进行加保护,等操作结束后再 ...

  3. JS事件处理函数中return false到底是什么东西

    在<JS DOM编程艺术>一书中,用return false来阻止事件默认行为,可是js高程3里没有这种用法,那这到底是什么呢. 先看一下知乎的一个解释 就此问题,首先要纠正两个观点: 1 ...

  4. ESB企业服务总线到底是什么东西呢?

    顾名思义,企业服务总线(ESB)就是一条企业架构的总线,所有的企业服务都挂接到该总线上对外公布,企业服务总线负责管理服务目录,解析服务请求者的请求方法.消息格式,并对服务提供者进行寻址,转发服务请求. ...

  5. iOS单元测试(作用及入门提升)

    由于只是一些简单实用的东西,学学还是挺不错的.其实单元测试用的好,开发起来也会快很多.单元测试对于我目前来说,就是为了方便测试一些功能是否正常运行,还有调试接口是否能正常使用.有时候你可能是为了测试某 ...

  6. 【iOS】单元测试

    iOS单元测试(作用及入门提升) 字数1704 阅读16369 评论26 喜欢247 由于只是一些简单实用的东西,学学还是挺不错的.其实单元测试用的好,开发起来也会快很多.单元测试对于我目前来说,就是 ...

  7. Java单元测试初体验(JUnit4)

    什么是单元测试 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在程序中只用到该函数的一小部分功能,并且经过调试可以确定,这一小部分功能是正确的.但是,我们同时应 ...

  8. laravel进行单元测试的时候如何模拟数据库以及mockery的调用

    单元测试是独立的,所谓的独立是指有独立的运行容器,独立的数据库. 这样做有什么好处呢? (1). 不会跟正常的容器产生冲突,继而影响正常业务. (2). 数据库独立防止数据被修改影响单元测试结果. 这 ...

  9. WmS详解(一)之token到底是什么?基于Android7.0源码

    做Android有些年头了,Framework层三大核心View系统,WmS.AmS最近在研究中,这三大块,每一块都够写一个小册子来介绍,其中View系统的介绍,我之前有一个系列的博客(不过由于时间原 ...

随机推荐

  1. mongodb状态

    基本信息 spock:PRIMARY>db.serverStatus() { "host" :"h6.corp.yongche.org", //主机名 & ...

  2. git 使用总结

  3. cookie使用随笔

    cookie是一种浏览器缓存,可以理解为存放在浏览器上的一种小文件. 大小数目限制: 不同浏览器对于大小和数目均有不一样的限制,大小一般为4k,数目从30~50个(视浏览器类型),以键值对的形式存储, ...

  4. Mybatis 一对一,一对多,多对一,多对多的理解

    First (一对一) 首先我来说下一对一的理解,就是一个班主任只属于一个班级,一个班级也只能有一个班主任.好吧这就是对于一对一的理解 怎么来实现呢? 这里我介绍了两种方式: 一种是:使用嵌套结果映射 ...

  5. MySQL、SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法. 可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应 ...

  6. iOS 开发者应该知道的 ARM 结构

    http://news.cnblogs.com/n/68903/ 我在写「NEON on iPhone 入门」的时候,曾以为读者已经比较了解 iOS设备的处理器知识.然而,看过网上的一些讨论,我才发现 ...

  7. AI 人工智能 探索 (七)

    我简单画了一幅图,来讲下 ai 中的设计模式.图形中的这些人物,我想大家都能看的明白. 当 盗贼出现,人们发现了他们,就 呼叫 主类,然后主类再  通知 下面对应的管理局,然后管理局再 分配人手过去 ...

  8. 函数FindFirstFile

    文件查找 Struct Lnode{ Char table[256];         //保存文件夹名 Struct Lnode*next; } 加文件夹入链表 Lnode *newList; Vo ...

  9. 平移关节(Prismatic Joint)

    package{ import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.Joints ...

  10. magento head.phtml 加载<a target=_parent

    <script language="javascript">function alinks(){  var links = document.getElementsBy ...