在功能测试方法中运行循环是否可以? [英] Is it okay to run for loops in functional test methods?

查看:69
本文介绍了在功能测试方法中运行循环是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试方法中运行循环是否可以(在概念上)?

Is it okay (conceptually) to run for loops in test methods?

我想在控制器中测试一系列参数值,以确定不同的输入是否返回正确的值.

I'd like to test a range of parameter values into a controller, to determine if the different inputs return the correct values.

  test "logged in user - add something - 0 qty" do
    @app = Factory.create(:app)

    (0..5).each do |t|
      @qty = t
      login(:user)
      get :add. :id => @app.id, :qty => @qty
      assert_nil(flash[:error])
      assert_response :redirect
      assert_redirect_to :controller => :apps, :action => :show, :id => @app.id
      if @qty = 0 
        assert_equal(Invite.all.count, @qty + 1)
      else 
        assert_something .........
    end
  end

类似的东西.

推荐答案

我通常尝试避免在测试代码中使用任何形式的条件语句或循环.您希望测试尽可能简单,如果开始在测试中包含逻辑,则必须测试它们以确保它们按设计工作.我将循环分解为单独的测试用例,这样,如果其中任何一个失败,则更容易准确地确定是什么输入导致了失败.如果测试失败,则应立即清楚是什么原因导致的.您不必分析测试代码就可以找出答案.

I usually try to avoid any kind of conditional statements or loops in test code. You want your tests to be as simple as possible, and if you start including logic in your tests you have to test them to make sure they work as designed. I would break the loop up into separate test cases, that way if any one of them fails it's easier to pinpoint exactly what inputs caused the failure. When a test fails it should be immediately obvious what caused it. You shouldn't have to analyze the test code to figure it out.

我确实想补充一点,在某些极其罕见情况下,您可能希望在测试用例中有一个循环.一个特定的示例是在测试并发问题时.这是一般规则的一个例外,您应该有一个很好的,很容易理解的理由来在测试中使用任何逻辑.

I do want to add that there are some extremely rare cases where you would want to have a loop in your test cases. One specific example is when you're testing for concurrency issues. This is an exception to the general rule, and you should have a very good and well-understood reason for having any kind of logic in your tests.

这篇关于在功能测试方法中运行循环是否可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆