You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel单元测试在BitBucket流水线中报错:/dev/tty需可读写

解决Laravel单元测试在BitBucket流水线中的TTY模式错误

问题说明

我在Laravel里做了个带文件读写操作的单元测试,本地跑完全正常,但放到BitBucket流水线执行时弹出这个错误:

...

  • php artisan test
    Warning: TTY mode requires /dev/tty to be read/writable.
    ...

我的BitBucket流水线配置文件bitbucket-pipelines.yml内容如下:

# This is a sample build configuration for PHP.
# Check our guides at https://confluence.atlassian.com/x/e8YWN for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: php:8.1.10

pipelines:
  pull-requests: #trigger on pullrequest, only works when there are no merge conflicts
    '**': #this runs as default for any branch not elsewhere defined
      - step:
          caches:
            - composer
          script:
            - apt-get update && apt-get install -y unzip git zip libzip-dev libfreetype6-dev libjpeg62-turbo-dev && docker-php-ext-install bcmath && docker-php-ext-install sockets && docker-php-ext-install gd && docker-php-ext-install zip
            - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
            - apt-get update && apt-get install -qy git curl libmcrypt-dev default-mysql-client
            - docker-php-ext-install pdo_mysql
            - composer install
            - ln -f -s .env.pipelines .env
            - php artisan migrate --force
            - php artisan db:seed --class=NewRollout --force
            - php artisan permissions:generate
            - php artisan serve &
            - sleep 5
            - php artisan test
            - curl -vk http://localhost:8000
            - vendor/bin/phpcs --warning-severity=8
          services:
            - mysql
...

修复方案

这个错误的根源是Laravel的test命令默认会尝试启用TTY交互模式,但BitBucket流水线环境里没有可读写的/dev/tty设备。直接改测试命令就能解决:

  • 执行测试时加上--no-interaction(简写-n)参数,禁用交互模式,绕开TTY要求:
php artisan test --no-interaction

另外还有几个优化点可以顺带调整:

  1. 合并重复的apt-get update:脚本里两次执行这个命令,合并成一次能减少构建耗时。
  2. 确保存储目录权限:如果测试涉及文件读写,执行测试前加一句权限设置:
chmod -R 775 storage bootstrap/cache
  1. 去掉不必要的Web服务启动:单元测试本身不需要依赖HTTP服务,完全可以删掉php artisan serve &和sleep 5这两步。

修改后,流水线脚本里的测试环节可以改成这样:

chmod -R 775 storage bootstrap/cache
php artisan test --no-interaction

内容的提问来源于stack exchange,提问作者Rik Verbeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 08:15:32