编写第一个单元测试
打赏作者

软件的软啊软件的件

非常支持站长啊~, 炒鸡激动, 之前还说PHPUnit 怎么学习没有思路, 想不到突然就有了,很开心支持站长~

mostwin

终于出来了!

andycc77e

終於出來了~

kissgxd

单元测试终于来了

making

终于出来了~~~~~~~~~~~~~

diguojin

很期待这个系列, 赞一个

GOD_Nt

测试

27631022 回复 GOD_Nt

测试下这个留言

chenze007

windows 环境的小伙伴们注意了 不用下载 phpunit
把 C:\laravel54\vendor\bin 加入到环境变量就好用了
注意你的php版本 建议7.0

JellyBool 回复 chenze007

你这几个回复很给力

Sssssssss_Lyw 回复 chenze007

0.0 添加了还是没什么用额

chenze007 回复 Sssssssss_Lyw
你要找对 你自己项目的路径 
或者直接cd进去 执行一下
如果是可以的 就是你的环境变量没有配置正确
736074781 回复 chenze007

请问为什么我不论是通过环境变量执行 vendor/bin/phpunit, 还是直接 cd 进去执行 phpunit,都是提示:
You need to set up the project dependencies using Composer:
composer install
You can learn all about Composer on https://getcomposer.org/.
@JellyBool

求辅助举报

我刚开始用单元测试,遇到个问题整了很久解决不了,就是我怎么测试ajax请求,因为业务需要,我在生产代码中判断了一下是否是ajax请求的数据,不是的话就报错,否则继续,$request->isAjax(),这个要怎么测,我束手无策了。

JellyBool 回复 求辅助举报

endpoint testing

你应该是可以直接使用 post 类似的方法去测试的

covey

站长的声音好有趣啊

JellyBool 回复 covey

啊哈,第一次有这样高的评价

dtf

非常非常赞~~~站长很给力!

JellyBool 回复 dtf

啊哈,真心感谢支持啊

不了了了了了之

好奇问什么有些人需要些vendor/bin/phpunit才ok,我后来在bash里面添加了alias才可以直接写phpunit

JellyBool 回复 不了了了了了之

因为没有全局安装 phpunit 的话,就是使用 vendor 目录的吧

假如_丶

有使用Windows的选手吗? 我直接执行 phpunit没有显示像视频那个样子啊,不知道phpunitdos怎么执行啊~

JellyBool 回复 假如_丶

vendor/bin/phpunit 这样呢

假如_丶 回复 JellyBool

vendor/bin/phpuinit 执行 phpunit 没有像视屏那样跑起来提示

Laravel Framework 5.4.31

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  clear-compiled       Remove the compiled class file
  down                 Put the application into maintenance mode
  env                  Display the current framework environment
  help                 Displays help for a command
  inspire              Display an inspiring quote
  list                 Lists commands
  migrate              Run the database migrations
  optimize             Optimize the framework for better performance
  serve                Serve the application on the PHP development server
  tinker               Interact with your application
  up                   Bring the application out of maintenance mode
 admin
  admin:install        Install the admin package
// 提示一堆命令啥的 - -
JellyBool 回复 假如_丶

你确定你没有拼错?这不是 artisan 的输出么

假如_丶 回复 JellyBool

确定… vendor/bin/phpuinit 执行 phpunit

JellyBool 回复 假如_丶

vendor/bin/phpunit 这样呢。。。。

假如_丶 回复 JellyBool
PHPUnit 3.7.28 by Sebastian Bergmann.

Usage: phpunit [switches] UnitTest [UnitTest.php]
       phpunit [switches] <directory>

  --log-junit <file>        Log test execution in JUnit XML format to file.
  --log-tap <file>          Log test execution in TAP format to file.
  --log-json <file>         Log test execution in JSON format.

  --coverage-clover <file>  Generate code coverage report in Clover XML format.
  --coverage-html <dir>     Generate code coverage report in HTML format.
  --coverage-php <file>     Serialize PHP_CodeCoverage object to file.
  --coverage-text=<file>    Generate code coverage report in text format.
                            Default to writing to the standard output.

  --testdox-html <file>     Write agile documentation in HTML format to file.
  --testdox-text <file>     Write agile documentation in Text format to file.

  --filter <pattern>        Filter which tests to run.
  --testsuite <pattern>     Filter which testsuite to run.
  --group ...               Only runs tests from the specified group(s).
  --exclude-group ...       Exclude tests from the specified group(s).
  --list-groups             List available test groups.
  --test-suffix ...         Only search for test in files with specified
                            suffix(es). Default: Test.php,.phpt

  --loader <loader>         TestSuiteLoader implementation to use.
  --printer <printer>       TestSuiteListener implementation to use.
  --repeat <times>          Runs the test(s) repeatedly.

  --tap                     Report test execution progress in TAP format.
  --testdox                 Report test execution progress in TestDox format.

  --colors                  Use colors in output.
  --stderr                  Write to STDERR instead of STDOUT.
  --stop-on-error           Stop execution upon first error.
  --stop-on-failure         Stop execution upon first error or failure.
  --stop-on-skipped         Stop execution upon first skipped test.
  --stop-on-incomplete      Stop execution upon first incomplete test.
  --strict                  Run tests in strict mode.
  -v|--verbose              Output more verbose information.
  --debug                   Display debugging information during test execution.

  --process-isolation       Run each test in a separate PHP process.
  --no-globals-backup       Do not backup and restore $GLOBALS for each test.
  --static-backup           Backup and restore static attributes for each test.

  --bootstrap <file>        A "bootstrap" PHP file that is run before the tests.
  -c|--configuration <file> Read configuration from XML file.
  --no-configuration        Ignore default configuration file (phpunit.xml).
  --include-path <path(s)>  Prepend PHP's include_path with given path(s).
  -d key[=value]            Sets a php.ini value.

  -h|--help                 Prints this usage information.
  --version                 Prints the version and exits.

现在显示的是这个…没执行啊…

假如_丶 回复 jialingfei

放弃了 - -

高永立

今天开始要学习测试了。一直没写过。

JellyBool 回复 高永立

挺好的。写得一手好的测试的话,完全不担心找工作问题