草庐IT

test_compare

全部标签

python - 测试代码是否从 py.test session 中执行

如果我的代码在py.test下运行,我想连接到不同的数据库。是否有可以调用的函数或可以测试的环境变量来告诉我是否在py.testsession下运行?处理这个问题的最佳方法是什么? 最佳答案 我想到了一个更简单的解决方案:importsysif"pytest"insys.modules:...Pytest运行程序将始终加载pytest模块,使其在sys.modules中可用。当然,此解决方案仅在您尝试测试的代码不使用pytest本身时才有效。 关于python-测试代码是否从py.tes

python - 测试代码是否从 py.test session 中执行

如果我的代码在py.test下运行,我想连接到不同的数据库。是否有可以调用的函数或可以测试的环境变量来告诉我是否在py.testsession下运行?处理这个问题的最佳方法是什么? 最佳答案 我想到了一个更简单的解决方案:importsysif"pytest"insys.modules:...Pytest运行程序将始终加载pytest模块,使其在sys.modules中可用。当然,此解决方案仅在您尝试测试的代码不使用pytest本身时才有效。 关于python-测试代码是否从py.tes

python - 在测试中创建和导入辅助函数,而不使用 py.test 在测试目录中创建包

问题如何在测试文件中导入辅助函数而不在test目录中创建包?上下文我想创建一个可以在多个测试中导入的测试辅助函数。说,像这样:#Incommon_file.pydefassert_a_general_property_between(x,y):#testaspecificrelationshipbetweenxandyassert...#Intest/my_test.pydeftest_something_with(x):some_value=some_function_of_(x)assert_a_general_property_between(x,some_value)使用Pyt

python - 在测试中创建和导入辅助函数,而不使用 py.test 在测试目录中创建包

问题如何在测试文件中导入辅助函数而不在test目录中创建包?上下文我想创建一个可以在多个测试中导入的测试辅助函数。说,像这样:#Incommon_file.pydefassert_a_general_property_between(x,y):#testaspecificrelationshipbetweenxandyassert...#Intest/my_test.pydeftest_something_with(x):some_value=some_function_of_(x)assert_a_general_property_between(x,some_value)使用Pyt

python - 查询时出现AttributeError : Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute

以下代码:Base=declarative_base()engine=create_engine(r"sqlite:///"+r"d:\foo.db",listeners=[ForeignKeysListener()])Session=sessionmaker(bind=engine)ses=Session()classFoo(Base):__tablename__="foo"id=Column(Integer,primary_key=True)name=Column(String,unique=True)classBar(Base):__tablename__="bar"id=Col

python - 查询时出现AttributeError : Neither 'InstrumentedAttribute' object nor 'Comparator' has an attribute

以下代码:Base=declarative_base()engine=create_engine(r"sqlite:///"+r"d:\foo.db",listeners=[ForeignKeysListener()])Session=sessionmaker(bind=engine)ses=Session()classFoo(Base):__tablename__="foo"id=Column(Integer,primary_key=True)name=Column(String,unique=True)classBar(Base):__tablename__="bar"id=Col

Python 单元测试 : Automatically Running the Debugger when a test fails

有没有办法在单元测试失败时自动启动调试器?目前我只是手动使用pdb.set_trace(),但这非常繁琐,因为我每次都需要添加它并在最后取出它。例如:importunittestclasstests(unittest.TestCase):defsetUp(self):passdeftest_trigger_pdb(self):#thisisthewayIdoitnowtry:assert1==0exceptAssertionError:importpdbpdb.set_trace()deftest_no_trigger(self):#thisisthewayIwouldliketodo

Python 单元测试 : Automatically Running the Debugger when a test fails

有没有办法在单元测试失败时自动启动调试器?目前我只是手动使用pdb.set_trace(),但这非常繁琐,因为我每次都需要添加它并在最后取出它。例如:importunittestclasstests(unittest.TestCase):defsetUp(self):passdeftest_trigger_pdb(self):#thisisthewayIdoitnowtry:assert1==0exceptAssertionError:importpdbpdb.set_trace()deftest_no_trigger(self):#thisisthewayIwouldliketodo

Comparable和Comparator的用法和区别

文章目录前言在这里给大家整理了一下comparable和comparator的用法和区别,这些在以后代码和面试中可能也会出现,那么,就跟着我一起去看看吧! 一.Comparable1.Comparable是什么?publicinterfaceComparablecomparable是个接口,此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方法。实现此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序2.comparable有用吗?有用的话它有什么用?在这里先给大家一个应用

python - 在 py.test 中的每个测试之前和之后运行代码?

我想在我的测试套件中的每个测试之前和之后运行额外的设置和拆卸检查。我查看了固定装置,但不确定它们是否是正确的方法。我需要在每次测试之前运行设置代码,并且需要在每次测试之后运行拆卸检查。我的用例是检查没有正确清理的代码:它会留下临时文件。在我的设置中,我将检查文件,在拆解中我还将检查文件。如果有额外的文件,我希望测试失败。 最佳答案 py.test固定装置是实现您的目的的技术上足够的方法。你只需要像这样定义一个fixture:@pytest.fixture(autouse=True)defrun_around_tests():#Cod