我有一个集成测试套件.我有一个IntegrationTestBase
课程,我的所有测试都要扩展.此基类具有@Before
(public void setUp()
)和@After
(public void tearDown()
)方法来建立API和DB连接.我一直在做的就是在每个测试用例中调用这两个方法并调用super.setUp()
和super.tearDown()
.但是,如果有人忘记调用父类或者把它们放在错误的地方,并抛出一个异常,他们会忘记在最后什么超级调用此可能会出现问题.
我想要做的是在基类上创建setUp
和tearDown
方法final
,然后添加我们自己的注释@Before
和@After
方法.做一些初始测试似乎总是按此顺序调用:
Base @Before
Test @Before
Test
Test @After
Base @After
Run Code Online (Sandbox Code Playgroud)
但我只是担心订单无法保证并且可能会导致问题.我环顾四周,没有看到任何关于这个问题的内容.有谁知道我能做到这一点而没有任何问题吗?
码:
public class IntegrationTestBase {
@Before
public final void setUp() { *always called 1st?* }
@After
public final void tearDown() { *always called last?* }
}
public class MyTest extends IntegrationTestBase {
@Before
public final void before() { *always called …
Run Code Online (Sandbox Code Playgroud) 在 Rust 中,有没有办法在使用标准测试库运行所有测试后(即在cargo test
.
我不打算在每次测试后运行拆卸功能,因为它们已在这些相关帖子中讨论过:
这些讨论了运行的想法:
std::panic::catch_unwind
)std::sync::Once
)一种解决方法是环绕cargo test
调用的 shell 脚本,但我仍然很好奇上述方法是否可行。
我想测试一个与主机系统交互的Elixir模块,并且具有副作用的方法.对于这个问题并保持简短,假设它是几个目录的创建.这些目录当然应该在运行测试后删除,如果测试(很长时间)由于任何原因(坏的模块代码,错误的测试代码等)而失败.
我想知道如何最好/最优雅地解决这个清理步骤.我查看了ExUnit.Callbacks.on_exit/2的文档,但它的示例仅用于设置和简单拆解(不涉及传递状态).我也在线搜索,但没有发现任何有用的东西,所以可能是我的想法本身并不好 - 我也愿意接受重构问题的建议.
defmodule SimpleTest do
use ExUnit.Case
setup_all do
ts = Time.utc_now |> Time.to_string
{:ok, [timestamp: ts]}
# paths to be cleaned are not yet known here
end
test "first test", context do
path = "/tmp/dir" <> context[:timestamp]
assert :ok == SimpleModule.mkdir(path)
assert :eexist == SimpleModule.mkdir(path)
# [path] should be passed on to cleanup
end
test "second test", context do
path = "/tmp/dir" <> context[:timestamp]
path2 = "/tmp/dir2" <> context[:timestamp]
SimpleModule.mkdir(path)
SimpleModule.mkdir(path2)
assert File.exists?(path) …
Run Code Online (Sandbox Code Playgroud) jest
提供afterEach
,beforeEach
,afterAll
和beforeAll
要完成安装和拆卸的逻辑。我想做的是在一次特定测试后清理。考虑以下:
describe("a family of tests it makes sense to group together", () => {
...
test("something I want to test", () => {
// some setup needed for just this test
global.foo = "bar"
// the test
expect(myTest()).toBe(true)
// clear up
delete global.foo
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果上述测试由于某种原因失败,则delete global.foo
永远不会运行。这意味着它之后的所有测试可能都会失败。我没有看到 1 个测试失败,而是看到大量测试失败,这可能会令人困惑。
一种解决方案是添加delete global.foo
到我的afterEach
. 不需要在每次测试后都运行它,但它也没有任何危害。另一种解决方案是单独放置特定的测试,以便afterEach
仅适用于它。但这似乎也不理想 - 如果该测试属于其他测试,那么它就不可能与它们一起存在。
有没有办法只为特定测试运行拆卸逻辑(而不在实际测试中运行它)。在我的特定用例中,第一个概述的解决方案很好,但我可以想象可能存在需要更细粒度控制的情况。例如,如果我的拆卸方法需要很长时间,我就不想重复很多次,因为这会减慢整个测试套件的速度。
我正在使用NUnit.我的测试方法定义为:
[Test]
[TestCase("Fred", "Bloggs")]
[TestCase("Joe", "Smith")]
public void MyUnitTest(string firstName, string lastName)
{
...
}
Run Code Online (Sandbox Code Playgroud)
TestCase完成后,它进入TearDown方法.想要做的是将那些TestCase参数传递给测试方法,但也传递给TearDown方法.
像这样的东西:
[TearDown]
public void TearDown(string firstName, string lastName)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我希望NUnit支持这种开箱即用的功能.否则,我需要在测试方法中编写定制代码,以将测试数据存储在集合中.然后在TearDown方法中使用该集合.
如果有人有任何想法..会很棒!谢谢.基督教
我正在尝试创建一组新的测试来测试我正在处理的遗留网站.该站点在后端使用数据库.我打算使用SpecFlow和Selenium,但是我对处理清理数据的最佳方法感到有点困惑.
目前,我有一组数据库备份,其中包含一组样本数据,我会在每次测试运行之前将其恢复.然而,这很麻烦,因此我只想在发布之前对关键测试运行执行此操作,并使持续集成运行在两者之间的同一数据库上运行.
目前我有大量的测试,如下所示:
Secenario: Test Item Creation
Given I am logged in
When I create an item with a unique name
Then an item exists with the unique name
Run Code Online (Sandbox Code Playgroud)
when步骤使用GUID来确保名称是唯一的,然后步骤可以通过模块变量访问它以检查它是否存在.
就像我说的那样,我有很多类似的测试,我在同一个数据库上多次运行它们,所以测试系统充满了物品,这会减慢搜索速度等.
我的问题是处理这个问题的最佳方法是什么?我应该在测试中创建另一个步骤,再次删除该项目,如下所示:
Secenario: Test Item Creation
Given I am logged in
When I create an item with a unique name
Then an item exists with the unique name
Then delete the item with the unique name
Run Code Online (Sandbox Code Playgroud)
或者我的测试框架应该以某种方式处理这个问题?如果是这样,人们做什么?鉴于SpecFlow步骤的全局性,我认为如果具有父子关系的多个项目可能成为问题,则以正确的顺序获取拆除步骤.
我正在使用Mock从具有特定返回值的类替换方法.它工作得很好,也许有点太好......我这样做(见下文),但是在下一个测试类中,我重复使用密码类而不进行模拟,并且放置在该测试中的模拟仍然有效.
from utils import password as pass_helper
class TestPassword(unittest.TestCase):
def setUp(self):
self.username = "user"
self.password = "Test_1234_pass"
pass_helper._get_password_from_keyboard = Mock(return_value=self.password)
def test_password(self):
password = pass_helper._get_password_from_keyboard(self.username)
self.assertEqual(password, self.password)
Run Code Online (Sandbox Code Playgroud)
我尝试通过这样的方式撤消TearDown方法中的mock,但它不起作用.
pass_helper._get_password_from_keyboard = pass_helper._get_password_from_keyboard
Run Code Online (Sandbox Code Playgroud)
如何恢复类方法的原始功能?
在每次"基于文件的集成"测试结束时,我想清除相关文件的临时文件夹.
如果测试失败,我想将文件保留在那里,这样我就可以查看意外的输出.
Google Test TearDown中是否有办法检查测试是否失败?
假设在所有测试中执行setup
或者teardown
相同的操作可能是昂贵的操作,并且在测试运行期间其结果不会被弄乱.在每次测试之前/之后让它们运行似乎是不对的.
那么有没有一种首选的方法只在第一次测试执行之前运行设置/拆卸代码,并且只在最后一次测试运行之后?
编辑:我正在处理的特定情况应该测试Net :: FTP的一些扩展,从而建立一个FTP连接并设置一些远程对象进行测试:
class TestFTPExtensions < Test::Unit::TestCase
def setup
# Setup connection
@ftp = Net::FTP.new 'localhost', 'anonymous'
@ftp.passive = true
# Create remote test directory
@ftp.mkdir 'dir'
# Create remote test file
path = File.join Dir.tmpdir, 'file'
File.open path, 'w' do |f|
@ftp.put f
end
File.delete path
end
def teardown
@ftp.rmdir 'dir'
@ftp.delete 'file'
@ftp.close
end
# imagine some tests here that don't change/remove any remote objects
end
Run Code Online (Sandbox Code Playgroud) 我至少使用 pytest 作为通用测试运行程序,对工作中的各种 API 产品进行大型自动化集成测试,并且我一直在尝试找到一个同样通用的拆卸函数示例,该函数在任何测试完成时运行,无论测试成功或失败失败。
我的典型使用模式是超线性的,通常是这样的:
def test_1():
<logic>
assert something
def test_2():
<logic>
assert something
def test_3():
<logic>
assert something
Run Code Online (Sandbox Code Playgroud)
有时,当这样做有意义时,我会在脚本的顶部放入一个设置装置,并将自动使用参数设置为“True”,该参数在每个脚本启动时运行:
@pytest.fixture(scope="session", autouse=True)
def setup_something():
testhelper = TestHelper
testhelper.create_something(host="somehost", channel="somechannel")
def test_1():
<logic>
assert something
def test_2():
<logic>
assert something
def test_3():
<logic>
assert something
Run Code Online (Sandbox Code Playgroud)
直到最近,一次性 Docker 环境让我能够跳过整个拆卸过程,但我有点紧张,其中一个目前不可用。理想情况下,在不偏离我已经使用的相同线性模式的情况下,我将如何实现另一个 pytest 夹具,其功能如下:
@pytest.fixture
def teardown():
testhelper = TestHelper
testhelper.delete_something(thing=something)
Run Code Online (Sandbox Code Playgroud)
运行何时完成?
teardown ×10
unit-testing ×5
installation ×2
testing ×2
c# ×1
database ×1
elixir ×1
ex-unit ×1
fixtures ×1
googletest ×1
java ×1
javascript ×1
jestjs ×1
junit ×1
mocking ×1
nunit ×1
nunit-2.5 ×1
pytest ×1
python-2.7 ×1
ruby ×1
rust ×1
scenarios ×1
specflow ×1
test-data ×1
testunit ×1