简体   繁体   English

如何通过小袋鼠自动化浏览器回滚对数据库所做的更改

[英]How to rollback changes that has made in the database by wallaby automation browser

I have long test that call a lot of functions that test the all application.我进行了长时间的测试,调用了许多测试所有应用程序的函数。 I looking for a way that in case of failure in the test, I can rollback the specific changes that the automation did on the test.我正在寻找一种方法,如果测试失败,我可以回滚自动化对测试所做的特定更改。

For example:例如:

test "add user and login", session do
  session
  |> add_user()
  # There can be more functions here...
end

def add_user(session, loops // 2) do
  try do
    session
    |> visit("example.com")
    |> fill_in(css("#user_name", with "John Doe")
    |> click(css("#add_user_button"))
    |> assert_has(css("#user_added_successfully_message")
  rescue
    msg -> if loops > 0, do: add_user(session, loops - 1), else: raise msg 
  end
end

In case of failure on the assert_has function (the user aded but the message don't show up), I want to rollback all the changes that happened on the database before the add_user function called again in the rescue.如果 assert_has 函数失败(用户已添加但消息未显示),我想回滚在救援中再次调用 add_user 函数之前数据库上发生的所有更改。

In case you use Ecto to access the DB everywhere, you can use its sandbox mode .如果您使用Ecto到处访问数据库,您可以使用它的沙箱模式

Most likely you want to configure the sandbox pool in your config/test.exs (if not already there):很可能您想在config/test.exs配置沙箱池(如果还没有):

config :my_app, Repo, # my_app being the name of the application that holds the Repo
  pool: Ecto.Adapters.SQL.Sandbox

Then in your test_helper or tests do something like this:然后在你的 test_helper 或测试中做这样的事情:

setup do
  :ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
  Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end

With this, all your tests run in separate transactions and are rolled back afterwards.这样,您的所有测试都在单独的事务中运行,并在之后回滚。 Also the above makes sure that all the processes use the same connection and see the same transaction data (see :shared mode in the docs).此外,以上确保所有进程使用相同的连接并查看相同的事务数据(请参阅文档中的:shared模式)。 This example is taken from the the docs where there is more information on that.此示例取自文档,其中有更多相关信息。

If you can not use Ectos sandbox mode for whatever reason, a good option could be to start a database transaction yourself and share the connection between your test and the code under test.如果由于某种原因您不能使用 Ectos 沙箱模式,一个不错的选择可能是自己启动数据库事务并共享您的测试和被测代码之间的连接。 That way you can manually roll back after each test.这样您就可以在每次测试后手动回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM