jjzjj

php - 记录代码接收错误

coder 2024-04-15 原文

我是 Codeception 的新手,我遇到了一个我无法解决的问题。我的测试套件中有大约 40 个测试,如果测试失败,我需要发送一封电子邮件说明失败的原因。例如,如果 Codeception 在页面上找不到导致测试失败的元素,我需要发送一封仅包含错误的电子邮件,如下所示:

Failed to verify emailing wish list behaves as expected in ThisClass::thisTest (/home/qauser/codeception_tests///acceptance-mobile/Wishlist/EmailWishlistCest.php) Couldn't see "Success!","//*[@id="wish-list-confirm-popup"]/div/div/div[1]/h4":

我不想发送完整的堆栈跟踪,只发送实际错误。有谁知道这是否可能?

最佳答案

Codeception 公开了一个有用的事件集合,这些事件将在这个用例中派上用场。看看 Customization: Events Codeception 的文档部分以获取更多信息。

我建议拦截该页面上描述的两个事件:

  • test.fail 事件,用于汇总有关每个失败测试的信息。
  • test.fail.print 事件,用于在 Codeception 完成测试套件并将其自己的失败摘要打印到屏幕时处理聚合数据(例如,通过发送摘要电子邮件) .

为此,您只需构建一个自定义事件处理程序类并将其注册为配置文件中的扩展:

# codeception.yml
extensions:
    enabled: [MyCustomEventHandler]

# MyCustomEventHandler.php
<?php
// Note: this was drafted using Codeception 2.0.  Some of the namespaces
// maybe different if you're using a more-recent version of Codeception.
class MyCustomEventHandler extends \Codeception\Platform\Extension
{
    /**
     * @var \Exception[]
     */
    protected $testFailures = [];

    /**
     * Maps Codeception events to method names in this class.
     *
     * Defining an event/method pair in this array essentially subscribes
     * the method as a listener for its corresponding event. 
     *
     * @var array
     */
    public static $events = [
        \Codeception\Events::TEST_FAIL       => 'singleTestJustFailed',
        \Codeception\Events::TEST_FAIL_PRINT => 'allTestFailuresAreBeingDisplayed',
    ];

    /**
     * This method will automatically be invoked by Codeception when a test fails.
     *
     * @param \Codeception\Event\FailEvent $event
     */
    public function singleTestJustFailed(\Codeception\Event\FailEvent $event)
    {
        // Here we build a list of all the failures. They'll be consumed further downstream.
        $this->testFailures[] = $event->getFail();
    }

    /**
     * This method will automatically be invoked by Codeception when it displays
     * a summary of all the test failures at the end of the test suite.
     */
    public function allTestFailuresAreBeingDisplayed()
    {
        // Build the email.
        $emailBody = '';
        foreach ($this->testFailures as $failure) {
            // Methods in scope include: $failure->getMessage(), $failure->getFile(), etc.
            $emailBody .= $failure->getMessage() . "\r\n";
        }

        // Now send the email!
    }
}

希望这对您有所帮助!

关于php - 记录代码接收错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41772084/

有关php - 记录代码接收错误的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  4. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  5. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  8. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  9. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  10. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

随机推荐