如何标记一个函数而不是pytest的测试? 我使用pytest基于张力量来测试一些代码。 定义了一个测试柜,以简单性,例如: 类测试柜(tf.test.testcase): #... 问题是tf.test.testcase提供了有用的fu ...

问题描述 投票:0回答:3

class TestCase(tf.test.TestCase): # ...

问题是提供一个有用的功能

tf.test.TestCase
,该功能被视为Pytest的测试方法,因为其名称始于

self.test_session()
.

结果Pytest报告比我根据

test_
方法定义的测试方法更成功的测试。
我使用以下代码跳过:
test_session()

在测试报告中会有一些“ S”,表明有一些跳过测试。

无论如何,我可以在不更改全球pytest测试发现规则的情况下标记一种确切的方法而不是测试方法?
    

收集测试项目后的filter felter felf误差:在测试目录中使用自定义收集挂钩创建一个

test_session

class TestCase(tf.test.TestCase): @pytest.mark.skip @contextmanager def test_session(self): with super().test_session() as sess: yield sess

conftest.py

仍将收集

# conftest.py def pytest_collection_modifyitems(session, config, items): items[:] = [item for item in items if item.name != 'test_session']

方法(您会在
python unit-testing tensorflow pytest
3个回答
4
投票
报告行中注意到),但不会以测试执行它们,而不会在测试运行中任何地方考虑它们。

相关:修复了
test_session

风格的测试
检查


您可以直接或通过编写简单的装饰器来设置
pytest
。后者的行为应与鼻子的
collected n tests
装饰工相似。
unittest


运行pytest时,这仅运行未标记的方法:

__test__ = False

这种行为记录了

Herey


4
投票
nottest

属性设置为

def nottest(obj):
    obj.__test__ = False
    return obj

class TestMyTest:

    def test_should_not_collect_1(self):
        assert False
    test_should_not_collect_1.__test__ = False

    @nottest
    def test_should_not_collect_2(self):
        assert False

    def test_should_collect(self):
        assert True


def test_should_not_collect_1():
    assert False
test_should_not_collect_1.__test__ = False

@nottest
def test_should_not_collect_2():
    assert False


def test_should_collect():
    assert True
.

i我通过其标题找到了这篇文章,而不是专门针对张力流,而是因为我想知道如何定义运行时不运行的辅助函数(我想使用辅助函数来重复使用代码,例如,特别是在单独的函数中进行实例化,通常是很好的做法)。我正在为那些想知道的人提供解决方案。

第一个解决方案是从

$ pytest test.py -v ====================================== test session starts ====================================== platform darwin -- Python 3.9.1, pytest-7.0.1, pluggy-1.0.0 -- /Users/lucaswiman/.pyenv/versions/3.9.1/bin/python3.9 cachedir: .pytest_cache rootdir: /private/tmp plugins: anyio-2.2.0 collected 2 items test.py::TestMyTest::test_should_collect PASSED [ 50%] test.py::test_should_collect PASSED [100%] ======================================= 2 passed in 0.04s =======================================
的子类中定义您的功能。就我而言,我想使用
__test__

的功能声明函数,即我无法做到这一点。 使用装饰器False

不是解决方案,因为即使在其他功能中调用也将跳过。
第二个解决方案也非常简单,不是使您的函数的名称以“ test”开头。

如果由于某种原因您仍然想使其从“测试”开始,但是它具有一个参数(除

unittest.main()
),则必须将其设置为默认值,因为
unittest.TestCase()
将尝试运行它而不会输入该参数的值,从而导致错误,从而被视为失败的测试。我发现的一种解决方案是设置不是用例的默认值(在我的情况下,工作),并通过以下方式启动函数代码

unittest.TestCase

0
投票
您要测试此功能的供应:

self.subTest()

然后使用此测试代码:

@unittest.skip
通过运行
self
我将获得以下输出,其中说明了我暴露的情况:

unittest

特别是,我们看到

None
没有尝试运行

if param is None: self.skipTest()

本身,但它运行

def my_fun(param1=1, param2=1): return(param1 / param2)

成功地称其为“它”。

Finally, note that in unittest discovery feature, you can choose which file naming convention to use : 
https://docs.python.org/3/library/unittest.html#test-discovery
 with parameter 
# test_my_fun.py import unittest class MyFunTestCase(unittest.TestCase): @unittest.skip('Skip aux_fun_skipped') def aux_fun_skipped(self): print("This is aux_fun_skipped.") def test_aux_fun_with_param_failing(self, param): print("This is aux_fun_with_params_failing, param={}".format(param)) def test_aux_fun_with_param(self, param=None): if param is None: self.skipTest('Skipping as param is None') else: print("This is test_aux_fun_with_param, param={}".format(param)) def aux_fun(self, param1, param2): with self.subTest(param1=param1): my_fun(param1=param1) with self.subTest(parm2=param2): my_fun(param2=param2) with self.subTest(param1=param1, param2=param2): my_fun(param1=param1, param2=param2) def test_something_relying_on_aux_fun_skipped(self): self.aux_fun_skipped() print("Call done.") def test_something_relying_on_aux_fun(self): self.test_aux_fun_with_param(4) for param1 in [5, 6]: for param2 in ([10, 11]): self.aux_fun(param1, param2) self.test_aux_fun_with_param_failing(3) print("Did all calls.") if __name__ == '__main__': unittest.main()
 , and not only use files whose name starts with 
$ python3 -m unittest -v

, but AFAIK you don't have something similar for names of test functions inside a subclass of
test_aux_fun_with_param (test_ignore_test.MyFunTestCase) ... skipped 'Skipping as param is None'
test_aux_fun_with_param_failing (test_ignore_test.MyFunTestCase) ... ERROR
test_something_relying_on_aux_fun (test_ignore_test.MyFunTestCase) ... This is test_aux_fun_with_param, param=4
This is aux_fun_with_params_failing, param=3
Did all calls.
ok
test_something_relying_on_aux_fun_skipped (test_ignore_test.MyFunTestCase) ... skipped 'Skip aux_fun_skipped'

======================================================================
ERROR: test_aux_fun_with_param_failing (test_ignore_test.MyFunTestCase)
----------------------------------------------------------------------
TypeError: MyFunTestCase.test_aux_fun_with_param_failing() missing 1 required positional argument: 'param'

----------------------------------------------------------------------
Ran 4 tests in 0.002s

FAILED (errors=1, skipped=2)
.

我希望这有帮助。

在Unittest上做到这一点
unittest

tf.test具有skiptest(原因),请访问更多信息:


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.