zoukankan      html  css  js  c++  java
  • php单元测试到底是什么东西呢?

    前言: 真正写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.

    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.

    .........

  • 相关阅读:
    IE10标准模式不支持HTC (Html Components) ,升级重写Htc原有代码之一: 自定义属性
    Javascript 数组使用方法总结(转载)
    IE6IE9兼容性问题列表及解决办法_补充之六:锁表头的JQuery方案和非JQuery方案。(不支持IE6,7,8)
    软件国际化总结之一:数字与字符串之间的格式化和转化处理
    测试当前IE浏览器文档模型版本的js代码(使用documenMode)
    IE6IE9兼容性问题列表及解决办法_补充之五:在IE9下, disabled的文本框内容被选中后,其他控件无法获得焦点问题
    《从程序员到软件设计师的标志》(2010/05/10)
    《需求是软件设计师永远的痛》(2010/05/13)
    EOM与软件设计师开场白(2010/5/07)
    《软件设计师与程序员之间的拥抱和摆脱》(20101/05/11)
  • 原文地址:https://www.cnblogs.com/oxspirt/p/5801397.html
Copyright © 2011-2022 走看看