我在凤凰应用程序中进行了以下测试:
machineAddresses <- list(
list(host='rserver1',user='ubuntu',
ncore=11),
list(host='rserver2',user='ubuntu',
ncore=11)
)
spec <- lapply(machineAddresses,
function(machine) {
rep(list(list(host=machine$host,
user=machine$user)),
machine$ncore)
})
spec <- unlist(spec,recursive=FALSE)
library("doParallel")
cl <- makeCluster(type='PSOCK',master=primary,spec=spec)
registerDoParallel(cl)
#this is purely based on your need , there are many articles on how to run parallel loops , the focus is mainly on multiple hosts
clusterExport(cl, varlist=ls(.GlobalEnv))
print(cl)
##run your commands
stopCluster(cl)
这些行在测试中重复:
defmodule TattooBackend.Web.API.V1.PasswordControllerTest do
use TattooBackend.Web.ConnCase, async: true
use Bamboo.Test
alias TattooBackend.Web.Endpoint
alias TattooBackend.Accounts.Account
alias TattooBackend.Repo
alias Phoenix.Token
alias Comeonin.Bcrypt
import TattooBackend.AuthenticationRequiredTest
test "without passwords", %{conn: conn} do
account = insert(:account)
token = Token.sign(Endpoint, "account_id", account.id)
conn =
conn
|> put_req_header("authorization", "#{token}")
|> post(api_v1_password_path(conn, :create))
assert json_response(conn, 422)
end
test "with wrong password confirmation", %{conn: conn} do
account = insert(:account)
token = Token.sign(Endpoint, "account_id", account.id)
params = %{password: "new-password", password_confirmation: "password"}
conn =
conn
|> put_req_header("authorization", "#{token}")
|> post(api_v1_password_path(conn, :create, params))
assert json_response(conn, 422)
end
end
知道如何让它变干吗?
答案 0 :(得分:3)
如果要在所有测试中执行这两行,可以将重复代码放在setup
回调中,然后使用模式匹配来获取每个测试中的那些:
defmodule TattooBackend.Web.API.V1.PasswordControllerTest do
...
setup do
account = insert(:account)
token = Token.sign(Endpoint, "account_id", account.id)
[account: account, token: token]
end
test "without passwords", %{conn: conn, account: account, token: token} do
conn =
conn
|> put_req_header("authorization", "#{token}")
|> post(api_v1_password_path(conn, :create))
assert json_response(conn, 422)
end
test "with wrong password confirmation", %{conn: conn, account: account, token: token} do
params = %{password: "new-password", password_confirmation: "password"}
conn =
conn
|> put_req_header("authorization", "#{token}")
|> post(api_v1_password_path(conn, :create, params))
assert json_response(conn, 422)
end
end
setup
将在每次测试之前执行其代码,然后将account
和token
传递给每个测试。