草庐IT

test_slice

全部标签

Python 文档测试 : skip a test conditionally

我知道如何使用#doctest:+SKIP跳过doctest,但我不知道如何根据运行时条件有时跳过测试.例如:>>>ifos.path.isfile("foo"):...open("foo").readlines()...else:...pass#doctest:+SKIP['hello','world']这就是我想做的事情。我也会接受运行测试的解决方案,但如果不满足条件(即无条件运行测试但修改预期结果),则将预期结果更改为带有回溯的异常。 最佳答案 如果您不想对输出进行测试,您可以返回一个特殊值。让我们调用_skip这个特殊值:如

python - 如果 py.test 的另一个测试失败,我该如何跳过测试?

假设我有这些测试函数:deftest_function_one():assert#etc...deftest_function_two():#shouldonlyruniftest_function_onepassesassert#etc.如何确保test_function_two仅在test_function_one通过时运行(我希望这是可能的)?编辑:我需要这个,因为测试二正在使用测试一验证的属性。 最佳答案 您可以使用名为pytest-dependency的pytest插件.代码可以是这样的:importpytest@pyte

python - py.test : Show local variables in Jenkins

到目前为止,我们通过Jenkins调用py.test。如果测试失败,我们会看到像这样的通常的堆栈跟踪Traceback(mostrecentcalllast):File"/home/u/src/foo/bar/tests/test_x.py",line36,intest_schema_migrationserrors,out))AssertionError:Unknownoutput:["Migrationsfor'blue':",...]如果我能像在Django调试页面中那样看到局部变量(参见https://djangobook.com/wp-content/uploads/figu

python - 没有输出,即使有 `py.test -s`

我想将py.test与hunter结合使用:PYTHONHUNTER="module_startswith='foo'"py.test-s-ktest_bar不幸的是,hunter的输出(trace)不可见。版本:foo_cok_d@aptguettler:~$py.test--versionThisispytestversion3.4.2,importedfrom/home/foo_cok_d/local/lib/python2.7/site-packages/pytest.pycsetuptoolsregisteredplugins:pytest-xdist-1.22.2at/ho

python - 在 sklearn.cross_validation 中使用 train_test_split 和 cross_val_score 的区别

我有一个包含20列的矩阵。最后一列是0/1标签。数据链接是here.我正在尝试使用交叉验证在数据集上运行随机森林。我使用两种方法来做到这一点:使用sklearn.cross_validation.cross_val_score使用sklearn.cross_validation.train_test_split当我做我认为几乎完全相同的事情时,我得到了不同的结果。为了举例说明,我使用上述两种方法运行双重交叉验证,如下面的代码所示。importcsvimportnumpyasnpimportpandasaspdfromsklearnimportensemblefromsklearn.me

python - 在 Flask-SQLAlchemy 中隔离 py.test 数据库 session

我正在尝试使用Flask-SQLAlchemy构建一个Flask应用程序;我使用pytest来测试数据库。其中一个问题似乎是在不同测试之间创建隔离的数据库session。我编写了一个最小的完整示例来突出问题,请注意test_user_schema1()和test_user_schema2()是相同的。文件名:test_db.pyfrommodelsimportUserdeftest_user_schema1(session):person_name='FranClan'uu=User(name=person_name)session.add(uu)session.commit()ass

python - py.test : format failed assert AND print custom message

py.testassertdocs说...ifyouspecifyamessagewiththeassertionlikethis:asserta%2==0,"valuewasodd,shouldbeeven"thennoassertionintrospectiontakesplacesatallandthemessagewillbesimplyshowninthetraceback.Python的内置unittest模块也执行此操作,除非您的TestCase设置longMessage=True.拥有漂亮的断言格式对测试开发人员友好,而自定义消息对业务需求/人性化更友好。当您不在测试上

Python 列表/数组 : disable negative indexing wrap-around in slices

虽然我发现负数环绕(即A[-2]索引倒数第二个元素)在许多情况下非常有用,但当它发生在切片内部时,它通常更多与其说是有用的功能,不如说是一种烦恼,我经常希望有一种方法来禁用该特定行为。下面是一个固定的2D示例,但我对其他数据结构和其他维数也有过几次同样的不满。importnumpyasnpA=np.random.randint(0,2,(5,10))deffoo(i,j,r=2):'''sumofneighbourswithinrstepsofA[i,j]'''returnA[i-r:i+r+1,j-r:j+r+1].sum()在上面的切片中,我宁愿切片的任何负数都被视为与None相同

python - 类型错误 : unhashable type: 'slice' for pandas

我有一个pandas数据结构,我是这样创建的:test_inputs=pd.read_csv("../input/test.csv",delimiter=',')它的形状print(test_inputs.shape)这是(28000,784)我想打印其行的子集,如下所示:print(test_inputs[100:200,:])print(test_inputs[100:200,:].shape)但是,我得到:TypeError:unhashabletype:'slice'知道哪里出了问题吗? 最佳答案 pandas中的索引确实令

python - 单元 : stop after first failing test?

我在我的测试框架中使用了以下代码:testModules=["test_foo","test_bar"]suite=unittest.TestLoader().loadTestsFromNames(testModules)runner=unittest.TextTestRunner(sys.stdout,verbosity=2)results=runner.run(suite)returnresults.wasSuccessful()有没有办法让报告(runner.run?)在第一次失败后中止以防止过于冗长? 最佳答案 问题提出九年