首页 \ 问答 \ 以编程方式在pytest插件中注册fixture(Programmaticly register a fixture in a pytest plugin)

以编程方式在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 for pytest_sessionstart. This hook is executed at the begining of the test session, and I can see the printed output as expected. However, the new_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
更新时间:2022-04-27 15:04

最满意答案

所有我发现接近产生这样一个库的是这个博客: 使用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;
    }

相关问答

更多
  • 由于您需要将数据处理成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 ...
  • 对于在插入此实体时要检索的实体成员的数据库生成的值,下列之一应该为真: 此成员的“自动生成值”属性设置为true; 此成员的“自动同步”属性设置为“OnInsert”或“始终”。 请检查是否满足这些条件。 另外,请尝试更新到dotConnect for Oracle的6.30.160版本(如果您使用的是先前版本)。 如果问题依然存在,请给我们一个可以复制的测试项目吗? Well, I found the problem and it had nothing to do with devart. The pr ...
  • 看起来批量插入时无法保证订单。 所以我在目标表中添加了一个临时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 ...
  • 看起来像是异步的问题。 如果我错了,请告诉我,但我注意到你在一个using语句中有你的SqlBulkCopy和ObjectReader ,这很好,但是,你是异步进行所有的处理。 一旦你调用它并开始工作,你的using语句就会处理你的对象,这也会破坏你的连接。 奇怪的是,它听起来有时会起作用,但也许它只是在那时成为一种竞争条件。 Looks like an issue with it being Async. Please let me know if I wrong, but what I noticed ...
  • 您正在更新的数据是否会影响其他计算机查询的结果? 如果它然后它应该阻止它们,与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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)