可以使用Minitest从另一个测试中调用一个测试吗?

时间:2019-05-21 03:08:38

标签: ruby-on-rails

我有一些冗余代码,尤其是在上一个测试中,如果我可以通过仅在该上一个测试中调用其他测试来清理它,那就太好了。

test "found login element successfully" do
  user = @driver.find_element(name: 'username')
  assert_not_nil user, "found username element"
end

test "entered login successfully" do
  user = @driver.find_element(name: 'username')
  user.send_keys "test"
  assert_equal "test", user.attribute("value"), "username text entered successfully"
end

test "found password element successfully" do
  password = @driver.find_element(name: 'password')
  assert_not_nil password
end

test "entered password successfully" do
  password = @driver.find_element(name: 'password')
  password.send_keys "test*"
  assert_equal "test*", password.attribute("value"), "password text entered successfully"
end

test "logged in successfully" do
  user = @driver.find_element(name: 'username')
  user.send_keys "test"
  password = @driver.find_element(name: 'password')
  password.send_keys "test"
  password.submit
  sleep 5
  assert_equal 'reddit: the front page of the internet', @driver.title
end

1 个答案:

答案 0 :(得分:1)

关于minitest或任何其他基于ruby的测试库的美丽之处在于...它只是Ruby!因此,制定一种方法来完成您想要的事情:

def get_username
  @driver.find_element(name: 'username').attribute("value")
end 

def set_username(value)
  user = @driver.find_element(name: 'username')
  user.send_keys value
end

def get_password
  @driver.find_element(name: 'password').attribute("value")
end

def set_password(value)
  password = @driver.find_element(name: 'password')
  password.send_keys "test*"
end

# etc ... 

以上内容可以直接插入测试文件中,或者如果您想将其移动到自己的文件中,则可以通过require引入。