Rumah >pembangunan bahagian belakang >tutorial php >PHPUnit初试,phpunit_PHP教程

PHPUnit初试,phpunit_PHP教程

WBOY
WBOYasal
2016-07-12 08:50:111687semak imbas

PHPUnit初试,phpunit

先测试了一下加减,检查一下环境,又调用函数测试了服务器名。

源代码:

<span> 1</span> <span>class</span><span> DemoController extends \Think\Controller
</span><span> 2</span> <span>{
</span><span> 3</span> 
<span> 4</span>     <span>/*</span><span>*
</span><span> 5</span> <span>     * @assert (5, 8) == 13
</span><span> 6</span> <span>     * @assert (16, 76) == 92
</span><span> 7</span> <span>     * @assert (6, 16) == 32
</span><span> 8</span> <span>     * @assert (6, 4) == 0
</span><span> 9</span> <span>     * @assert ('abc', 1) == 2
</span><span>10</span> <span>     * @param int $a
</span><span>11</span> <span>     * @param int $b
</span><span>12</span> <span>     * @return int
</span><span>13</span>      <span>*/</span>
<span>14</span>     <span>public</span><span> function plus($a, $b)
</span><span>15</span> <span>    {
</span><span>16</span>         <span>return</span> $a +<span> $b;
</span><span>17</span> <span>    }
</span><span>18</span> 
<span>19</span>      <span>/*</span><span>*
</span><span>20</span> <span>     * @assert (14, 8) == 6
</span><span>21</span> <span>     * @assert (16, 6) == 10
</span><span>22</span> <span>     * @assert (6, 4) == 0
</span><span>23</span> <span>     * @assert ('45', 1) == 44
</span><span>24</span> <span>     * @param int $a
</span><span>25</span> <span>     * @param int $b
</span><span>26</span> <span>     * @return int
</span><span>27</span>      <span>*/</span>
<span>28</span>     <span>public</span><span> function subtract($a, $b)
</span><span>29</span> <span>    {
</span><span>30</span>         <span>return</span> $a -<span> $b;
</span><span>31</span> <span>    }
</span><span>32</span> 
<span>33</span>     <span>public</span> function connectToServer($serverName = <span>null</span><span>)
</span><span>34</span> <span>    {
</span><span>35</span>         <span>if</span> ($serverName == <span>null</span><span>) {
</span><span>36</span>             <span>throw</span> <span>new</span> Exception(<span>"</span><span>这不是一个服务器名</span><span>"</span><span>);
</span><span>37</span> <span>        }
</span><span>38</span>         $fp = fsockopen($serverName, <span>8080</span><span>);
</span><span>39</span>         <span>return</span> ($fp) ? <span>true</span> : <span>false</span><span>;
</span><span>40</span> <span>    }
</span><span>41</span> 
<span>42</span> 
<span>43</span> }

生成测试文件:

<span>  1</span> <span>class</span><span> DemoControllerTest extends \PHPUnit_Framework_TestCase
</span><span>  2</span> <span>{
</span><span>  3</span> 
<span>  4</span>     <span>/*</span><span>*
</span><span>  5</span> <span>     * @var DemoController
</span><span>  6</span>      <span>*/</span>
<span>  7</span>     <span>protected</span> $<span>object</span><span>;
</span><span>  8</span> 
<span>  9</span>     <span>/*</span><span>*
</span><span> 10</span> <span>     * Sets up the fixture, for example, opens a network connection.
</span><span> 11</span> <span>     * This method is called before a test is executed.
</span><span> 12</span>      <span>*/</span>
<span> 13</span>     <span>protected</span><span> function setUp()
</span><span> 14</span> <span>    {
</span><span> 15</span>         $<span>this</span>-><span>object</span> = <span>new</span><span> DemoController;
</span><span> 16</span> <span>    }
</span><span> 17</span> 
<span> 18</span>     <span>/*</span><span>*
</span><span> 19</span> <span>     * Tears down the fixture, for example, closes a network connection.
</span><span> 20</span> <span>     * This method is called after a test is executed.
</span><span> 21</span>      <span>*/</span>
<span> 22</span>     <span>protected</span><span> function tearDown()
</span><span> 23</span> <span>    {
</span><span> 24</span>         
<span> 25</span> <span>    }
</span><span> 26</span> 
<span> 27</span>     <span>/*</span><span>*
</span><span> 28</span> <span>     * Generated from @assert (5, 8) == 13.
</span><span> 29</span> <span>     *
</span><span> 30</span> <span>     * @covers Home\Controller\DemoController::plus
</span><span> 31</span>      <span>*/</span>
<span> 32</span>     <span>public</span><span> function testPlus()
</span><span> 33</span> <span>    {
</span><span> 34</span>         $<span>this</span>-><span>assertEquals(
</span><span> 35</span>                 <span>13</span>, $<span>this</span>-><span>object</span>->plus(<span>5</span>, <span>8</span><span>)
</span><span> 36</span> <span>        );
</span><span> 37</span> <span>    }
</span><span> 38</span> 
<span> 39</span>     <span>/*</span><span>*
</span><span> 40</span> <span>     * Generated from @assert (16, 76) == 92.
</span><span> 41</span> <span>     *
</span><span> 42</span> <span>     * @covers Home\Controller\DemoController::plus
</span><span> 43</span>      <span>*/</span>
<span> 44</span>     <span>public</span><span> function testPlus2()
</span><span> 45</span> <span>    {
</span><span> 46</span>         $<span>this</span>-><span>assertEquals(
</span><span> 47</span>                 <span>92</span>, $<span>this</span>-><span>object</span>->plus(<span>16</span>, <span>76</span><span>)
</span><span> 48</span> <span>        );
</span><span> 49</span> <span>    }
</span><span> 50</span> 
<span> 51</span>     <span>/*</span><span>*
</span><span> 52</span> <span>     * Generated from @assert (6, 16) == 32.
</span><span> 53</span> <span>     *
</span><span> 54</span> <span>     * @covers Home\Controller\DemoController::plus
</span><span> 55</span>      <span>*/</span>
<span> 56</span>     <span>public</span><span> function testPlus3()
</span><span> 57</span> <span>    {
</span><span> 58</span>         $<span>this</span>-><span>assertEquals(
</span><span> 59</span>                 <span>32</span>, $<span>this</span>-><span>object</span>->plus(<span>6</span>, <span>16</span><span>)
</span><span> 60</span> <span>        );
</span><span> 61</span> <span>    }
</span><span> 62</span> 
<span> 63</span>     <span>/*</span><span>*
</span><span> 64</span> <span>     * Generated from @assert (6, 4) == 0.
</span><span> 65</span> <span>     *
</span><span> 66</span> <span>     * @covers Home\Controller\DemoController::plus
</span><span> 67</span>      <span>*/</span>
<span> 68</span>     <span>public</span><span> function testPlus4()
</span><span> 69</span> <span>    {
</span><span> 70</span>         $<span>this</span>-><span>assertEquals(
</span><span> 71</span>                 <span>0</span>, $<span>this</span>-><span>object</span>->plus(<span>6</span>, <span>4</span><span>)
</span><span> 72</span> <span>        );
</span><span> 73</span> <span>    }
</span><span> 74</span> 
<span> 75</span>     <span>/*</span><span>*
</span><span> 76</span> <span>     * Generated from @assert ('abc', 1) == 0.
</span><span> 77</span> <span>     *
</span><span> 78</span> <span>     * @covers Home\Controller\DemoController::plus
</span><span> 79</span>      <span>*/</span>
<span> 80</span>     <span>public</span><span> function testPlus5()
</span><span> 81</span> <span>    {
</span><span> 82</span>         $<span>this</span>-><span>assertEquals(
</span><span> 83</span>                 <span>2</span>, $<span>this</span>-><span>object</span>->plus(<span>'</span><span>abc</span><span>'</span>, <span>1</span><span>)
</span><span> 84</span> <span>        );
</span><span> 85</span> <span>    }
</span><span> 86</span> 
<span> 87</span>     <span>/*</span><span>*
</span><span> 88</span> <span>     * Generated from @assert (14, 8) == 6.
</span><span> 89</span> <span>     *
</span><span> 90</span> <span>     * @covers Home\Controller\DemoController::subtract
</span><span> 91</span>      <span>*/</span>
<span> 92</span>     <span>public</span><span> function testSubtract()
</span><span> 93</span> <span>    {
</span><span> 94</span>         $<span>this</span>-><span>assertEquals(
</span><span> 95</span>                 <span>6</span>, $<span>this</span>-><span>object</span>->subtract(<span>14</span>, <span>8</span><span>)
</span><span> 96</span> <span>        );
</span><span> 97</span> <span>    }
</span><span> 98</span> 
<span> 99</span>     <span>/*</span><span>*
</span><span>100</span> <span>     * Generated from @assert (16, 6) == 10.
</span><span>101</span> <span>     *
</span><span>102</span> <span>     * @covers Home\Controller\DemoController::subtract
</span><span>103</span>      <span>*/</span>
<span>104</span>     <span>public</span><span> function testSubtract2()
</span><span>105</span> <span>    {
</span><span>106</span>         $<span>this</span>-><span>assertEquals(
</span><span>107</span>                 <span>10</span>, $<span>this</span>-><span>object</span>->subtract(<span>16</span>, <span>6</span><span>)
</span><span>108</span> <span>        );
</span><span>109</span> <span>    }
</span><span>110</span> 
<span>111</span>     <span>/*</span><span>*
</span><span>112</span> <span>     * Generated from @assert (6, 4) == 0.
</span><span>113</span> <span>     *
</span><span>114</span> <span>     * @covers Home\Controller\DemoController::subtract
</span><span>115</span>      <span>*/</span>
<span>116</span>     <span>public</span><span> function testSubtract3()
</span><span>117</span> <span>    {
</span><span>118</span>         $<span>this</span>-><span>assertEquals(
</span><span>119</span>                 <span>0</span>, $<span>this</span>-><span>object</span>->subtract(<span>6</span>, <span>4</span><span>)
</span><span>120</span> <span>        );
</span><span>121</span> <span>    }
</span><span>122</span> 
<span>123</span>     <span>/*</span><span>*
</span><span>124</span> <span>     * Generated from @assert ('abc', 1) == 0.
</span><span>125</span> <span>     *
</span><span>126</span> <span>     * @covers Home\Controller\DemoController::subtract
</span><span>127</span>      <span>*/</span>
<span>128</span>     <span>public</span><span> function testSubtract4()
</span><span>129</span> <span>    {
</span><span>130</span>         $<span>this</span>-><span>assertEquals(
</span><span>131</span>                 <span>44</span>, $<span>this</span>-><span>object</span>->subtract(<span>'</span><span>45</span><span>'</span>, <span>1</span><span>)
</span><span>132</span> <span>        );
</span><span>133</span> <span>    }
</span><span>134</span> 
<span>135</span>     <span>/*</span><span>*
</span><span>136</span> <span>     * @covers Home\Controller\DemoController::connectToServer
</span><span>137</span> <span>     * @todo   Implement testConnectToServer().
</span><span>138</span>      <span>*/</span>
<span>139</span>     <span>public</span><span> function testConnectToServer()
</span><span>140</span> <span>    {
</span><span>141</span> <span>//</span>        <span>//</span><span> Remove the following lines when you implement this test.
</span><span>142</span> <span>//</span><span>        $this->markTestIncomplete(
</span><span>143</span> <span>//</span><span>                'This test has not been implemented yet.'
</span><span>144</span> <span>//</span><span>        );</span>
<span>145</span>         $serverName = <span>'</span><span>wwwcom</span><span>'</span><span>;
</span><span>146</span>         $<span>this</span>->assertTrue($<span>this</span>-><span>object</span>->connectToServer($serverName) === <span>false</span><span>);
</span><span>147</span> <span>    }
</span><span>148</span>     <span>public</span><span> function testConnectToServer2()
</span><span>149</span> <span>    {
</span><span>150</span>         $serverName = <span>'</span><span>www.baidu.com</span><span>'</span><span>;
</span><span>151</span>         $<span>this</span>->assertTrue($<span>this</span>-><span>object</span>->connectToServer($serverName) !== <span>false</span><span>);
</span><span>152</span>     }

这里的服务器测试用例是手动加上去的!

执行结果:

<span> 1</span> ..FFF..F..F                                                       <span>11</span> / <span>11</span> (<span>100</span>%<span>)
</span><span> 2</span> 
<span> 3</span> Time: <span>44.42</span> seconds, Memory: <span>8</span><span>.75Mb
</span><span> 4</span> 
<span> 5</span> There were <span>5</span><span> failures:
</span><span> 6</span> 
<span> 7</span> <span>1</span><span>) Home\Controller\DemoControllerTest::testPlus3
</span><span> 8</span> Failed asserting that <span>22</span> matches expected <span>32</span><span>.
</span><span> 9</span> 
<span>10</span> D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:<span>67</span>
<span>11</span> 
<span>12</span> <span>2</span><span>) Home\Controller\DemoControllerTest::testPlus4
</span><span>13</span> Failed asserting that <span>10</span> matches expected <span>0</span><span>.
</span><span>14</span> 
<span>15</span> D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:<span>79</span>
<span>16</span> 
<span>17</span> <span>3</span><span>) Home\Controller\DemoControllerTest::testPlus5
</span><span>18</span> Failed asserting that <span>1</span> matches expected <span>2</span><span>.
</span><span>19</span> 
<span>20</span> D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:<span>91</span>
<span>21</span> 
<span>22</span> <span>4</span><span>) Home\Controller\DemoControllerTest::testSubtract3
</span><span>23</span> Failed asserting that <span>2</span> matches expected <span>0</span><span>.
</span><span>24</span> 
<span>25</span> D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:<span>127</span>
<span>26</span> 
<span>27</span> <span>5</span><span>) Home\Controller\DemoControllerTest::testConnectToServer2
</span><span>28</span> Failed asserting that <span>false</span> <span>is</span> <span>true</span><span>.
</span><span>29</span> 
<span>30</span> D:\wamp\www\wxportal\tests\Application\Home\Controller\DemoController.classTest.php:<span>158</span>
<span>31</span> 
<span>32</span> FAILURES!
<span>33</span> Tests: <span>11</span>, Assertions: <span>11</span>, Failures: <span>5</span><span>.
</span><span>34</span> 完成。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1134779.htmlTechArticlePHPUnit初试,phpunit 先测试了一下加减,检查一下环境,又调用函数测试了服务器名。 源代码: 1 class DemoController extends \Think\Controller 2 { 3...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn