以编程方式在pytest插件中注册fixture(Programmaticly register a fixture in a pytest plugin)
我正在寻找一种方法从Python运行一些pytest单元测试并动态注册pytest fixture。 正如Pytest文档中所解释的,当以编程方式运行测试时, 可以使用自定义插件更改其行为 。 我有以下设置
validation.py (包含要运行的测试)
def test_valid(new_fixture): assert new_fixture > 0
随着推出,
import pytest new_fixture_value = 36 class FixtureRegPlugin(object): def pytest_sessionstart(self): print('Test session start') @pytest.fixture def new_fixture(): return new_fixture_value pytest.main(['-sv', './validation.py'], plugins=[FixtureRegPlugin()])
这里我们使用自定义插件在
validation.py
运行测试,该插件为pytest_sessionstart
注册了一个钩子。 这个钩子在测试会话开始时执行,我可以按预期看到打印输出。 但是,new_fixture
未注册,因此测试失败并显示“找不到夹具”错误。目标是在运行时修改fixture的结果,因此我不能将其定义放在
validation.py
。I am looking for a way run some pytest unit tests from Python and register a pytest fixture dynamically. As explained in in the Pytest documentation, when running tests programmatically their behaviour can be altered with a custom plugin. I have the following setup
validation.py (contains the tests to run)
def test_valid(new_fixture): assert new_fixture > 0
which is launched with,
import pytest new_fixture_value = 36 class FixtureRegPlugin(object): def pytest_sessionstart(self): print('Test session start') @pytest.fixture def new_fixture(): return new_fixture_value pytest.main(['-sv', './validation.py'], plugins=[FixtureRegPlugin()])
here we run tests in
validation.py
with a custom plugin that registers a hook forpytest_sessionstart
. This hook is executed at the begining of the test session, and I can see the printed output as expected. However, thenew_fixture
is not registered, and so the test fails with a "fixture not found" error.The goal is to modify the result of the fixture at runtime and so I cannot just place its definition inside
validation.py
.
原文:https://stackoverflow.com/questions/46327646
最满意答案
所有我发现接近产生这样一个库的是这个博客: 使用LINQ to SQL批量更新和删除
这是迈向正确方向的一步
编辑:关于GetDeleteBatchCommand的评论。 它在源代码中。 这是一个代码:
private static DbCommand GetDeleteBatchCommand<TEntity>(this Table<TEntity> table, IQueryable<TEntity> entities) where TEntity : class { var deleteCommand = table.Context.GetCommand(entities); deleteCommand.CommandText = string.Format("DELETE {0}\r\n", table.GetDbName()) + GetBatchJoinQuery<TEntity>(table, entities); return deleteCommand; }
相关问答
更多-
3个具有相同ID但使用SqlBulkCopy更改不同列的记录(3 records with same ID but change different columns using SqlBulkCopy)[2021-09-28]
由于您需要将数据处理成DataTable (除非您正在编写自定义IDataReader ),您应该在将记录提供给SqlBulkCopy之前合并它们; 例如(伪代码): /* create empty data-table */ foreach(row in list) { var row = /* try to get exsiting row from data-table based on id */ if(row == null) { row = /* create and appen ... -
Devart InsertAllOnSubmit不会更新数据库生成的字段(Devart InsertAllOnSubmit doesn't update db generated fields)[2019-11-24]
对于在插入此实体时要检索的实体成员的数据库生成的值,下列之一应该为真: 此成员的“自动生成值”属性设置为true; 此成员的“自动同步”属性设置为“OnInsert”或“始终”。 请检查是否满足这些条件。 另外,请尝试更新到dotConnect for Oracle的6.30.160版本(如果您使用的是先前版本)。 如果问题依然存在,请给我们一个可以复制的测试项目吗? Well, I found the problem and it had nothing to do with devart. The pr ... -
SqlBulkCopy插入顺序(SqlBulkCopy insert order)[2023-08-27]
看起来批量插入时无法保证订单。 所以我在目标表中添加了一个临时id列。 流程如下: Step1: var dt = select *, id as tempId from server1.dbo.TableA order by id; Step2: SQL bulk copy into server2 bulkCopy.WriteToServer(dt); Step3: var resultDt = select top 4 id, tempId from server2.dbo.TableA order ... -
所有我发现接近产生这样一个库的是这个博客: 使用LINQ to SQL批量更新和删除 这是迈向正确方向的一步 编辑:关于GetDeleteBatchCommand的评论。 它在源代码中。 这是一个代码: private static DbCommand GetDeleteBatchCommand
(this Table table, IQueryable entities) where TEntity : class { var d ... -
SqlBulkCopy.WriteToServer()继续“连接已关闭”(SqlBulkCopy.WriteToServer() keep getting “connection is closed”)[2023-01-25]
看起来像是异步的问题。 如果我错了,请告诉我,但我注意到你在一个using语句中有你的SqlBulkCopy和ObjectReader ,这很好,但是,你是异步进行所有的处理。 一旦你调用它并开始工作,你的using语句就会处理你的对象,这也会破坏你的连接。 奇怪的是,它听起来有时会起作用,但也许它只是在那时成为一种竞争条件。 Looks like an issue with it being Async. Please let me know if I wrong, but what I noticed ... -
限制SqlBulkCopy的CPU(Limit CPU at SqlBulkCopy)[2023-07-16]
您正在更新的数据是否会影响其他计算机查询的结果? 如果它然后它应该阻止它们,与CPU无关,但因为记录被锁定以进行更新。 您希望操作尽可能快地运行,因此最大CPU是好的。 如果这两个数据集不同,那么没有理由为配置良好的SQL服务器无法同时服务这两个数据集,请查看调优选项,可用内存量等 Is the data you're updating affecting the results of the other machine's queries? If it is then it should block th ... -
基于@Kev回答,您可以批量插入到临时表中,然后可以触发存储过程。 我假设你对另一个表有一个FK约束,所以在插入目标表之前你需要这个值。 如果可能,您可以尝试删除约束。 然后执行批量插入,然后触发器可以稍后更新列。 Based on @Kev answer, you can do the bulk insert into a staging table and then you can have a trigger kick off your stored procedure. I'm assuming y ...
-
一个查询来确定需要插入什么,然后在一些内存中操作后,你可以InsertAllOnSubmit : using(MyDataContext db = new MyDataContext()) { var itemNames = Items.Select(i=>i.name).ToList(); var itemNamesInDb = db.Items.Where(i=>itemNames.Contains(i.Name)).Select(i=>i.Name).ToList(); var ...
-
MSDN文档中介绍了SQL批量复制事务行为。 如果你想要更精细的控制程度,一般的方法是将BCP转换成临时表,然后验证检查并从那里插入/更新到live。 SQL Bulk Copy transaction behaviour is covered in the MSDN documentation. If you want a more fine degree of control, the general method is to BCP into a temporary table, then valid ...
-
在我执行BulkCopy的系统中,我按照文档列映射 - SQL批量复制中的说明设置列映射 使用源列和目标列名称设置映射。 这个例子来自文档: Dim mapID As New _ SqlBulkCopyColumnMapping("ProductID", "ProdID") bulkCopy.ColumnMappings.Add(mapID) 当我第一次设置它时,我记得在没有明确设置环境中的列映射的情况下遇到麻烦。 In the system wher ...