我从PyUnit迁移到Pytest,我发现,与PyUnit不同,Pytest在运行测试(打印点)时不会在快速报告中区分测试报告中的失败和错误.怎么教Pytest做的呢?
看起来它只适用于使用Pytest执行的PyUnit测试,感谢flub的线索.
码:
import unittest
class TestErrorFail(unittest.TestCase):
def test_error(self):
raise Exception('oops')
def test_fail(self):
self.assertTrue(False)
Run Code Online (Sandbox Code Playgroud)
输出:
================================ test session starts =================================
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
plugins: django
collected 2 items
sometests.py FF
====================================== FAILURES ======================================
______________________________ TestErrorFail.test_error ______________________________
self = <sometests.TestErrorFail testMethod=test_error>
def test_error(self):
> raise Exception('oops')
E Exception: oops
sometests.py:5: Exception
______________________________ TestErrorFail.test_fail _______________________________
self = <sometests.TestErrorFail testMethod=test_fail>
def test_fail(self):
> self.assertTrue(False)
E AssertionError: False is not true
sometests.py:8: AssertionError
============================== 2 …Run Code Online (Sandbox Code Playgroud) 哇.我今晚发现使用该unittest模块编写的Python单元测试与模块下的覆盖率分析不trace相符.这是最简单的单元测试,在foobar.py:
import unittest
class Tester(unittest.TestCase):
def test_true(self):
self.assertTrue(True)
if __name__ == "__main__":
unittest.main()
Run Code Online (Sandbox Code Playgroud)
如果我运行它python foobar.py,我得到这个输出:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Run Code Online (Sandbox Code Playgroud)
大.现在我也想进行覆盖测试,所以我再次运行它python -m trace --count -C . foobar.py,但现在我得到了这个:
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Run Code Online (Sandbox Code Playgroud)
不,Python,它不行 - 你没有运行我的测试!似乎在trace某种程度上运行gums up unittest的测试检测机制.这是我提出的(疯狂)解决方案:
import unittest
class Tester(unittest.TestCase):
def test_true(self):
self.assertTrue(True)
class Insane(object):
pass
if __name__ == "__main__":
module = Insane()
for k, v in locals().items():
setattr(module, k, v) …Run Code Online (Sandbox Code Playgroud) 有没有办法检查python单元测试(或任何其他脚本),如果它是在PyCharm IDE内部执行?
我想在本地启动时在单元测试中做一些特殊的事情,当整个事情在构建服务器上执行时我不想做的事情.
干杯
在我的Python项目中,我们有大量的单元测试(数千个).虽然它们在逻辑上分布在文件和类之间,但我有时需要花费大量时间来查找它们,这些内容涵盖了我正在改变的功能.
当然,我可以从一些特定的文件/类中运行所有测试,但是由于大量的测试,再次运行它们会非常耗时(我在每次保存文件后都执行单元测试)我的IDE).
所以一般来说我需要一些解决方案,一次做以下活动:
有没有人对类似的东西有所了解?
python dependencies unit-testing code-coverage python-unittest
嗨我如何动态生成测试方法列表或文件数量.假设我在json中有file1,file2和filen以及输入值.现在我需要为下面的多个值运行相同的测试,
class Test_File(unittest.TestCase):
def test_$FILE_NAME(self):
return_val = validate_data($FILE_NAME)
assert return_val
Run Code Online (Sandbox Code Playgroud)
我使用以下命令运行py.test来生成html和junit报告
py.test test_rotate.py --tb=long --junit-xml=results.xml --html=results.html -vv
Run Code Online (Sandbox Code Playgroud)
目前我手动定义如下方法,
def test_lease_file(self):
return_val = validate_data(lease_file)
assert return_val
def test_string_file(self):
return_val = validate_data(string_file)
assert return_val
def test_data_file(self):
return_val = validate_data(data_file)
assert return_val
Run Code Online (Sandbox Code Playgroud)
请告诉我如何指定py测试以在报告时动态生成test_came方法.
我期待本博客中提到的确切内容" http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases "
但上面的博客使用unittest,如果我使用它,我无法生成html和junit报告
当我们使用如下夹具时,我得到的错误就像需要2个参数一样,
test_case = []
class Memory_utlization(unittest.TestCase):
@classmethod
def setup_class(cls):
fname = "test_order.txt"
with open(fname) as f:
content = f.readlines()
file_names = []
for i in content:
file_names.append(i.strip())
data = tuple(file_names)
test_case.append(data)
logging.info(test_case) # here test_case=[('dhcp_lease.json'),('dns_rpz.json'),]
@pytest.mark.parametrize("test_file",test_case) …Run Code Online (Sandbox Code Playgroud) 我正在使用python,unittest并希望编写一个测试,启动一些线程并等待它们完成.线程执行一个具有一些unittest断言的函数.如果任何断言失败,我希望测试失败.似乎并非如此.
编辑:最小的可运行示例(python3)
import unittest
import threading
class MyTests(unittest.TestCase):
def test_sample(self):
t = threading.Thread(target=lambda: self.fail())
t.start()
t.join()
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
输出是:
sh-4.3$ python main.py -v
test_sample (__main__.MyTests) ... Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 813, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 766, in run
self.__target(*self.__args, **self.__kwargs)
File "main.py", line 7, in <lambda>
t = threading.Thread(target=lambda: self.fail())
File "/usr/lib64/python2.7/unittest/case.py", line 450, in fail
raise self.failureException(msg)
AssertionError: None
ok
---------------------------------------------------------------------- …Run Code Online (Sandbox Code Playgroud) 根据unittest.TestLoader.discover的文档:
discover(start_dir, pattern=’test*.py’, top_level_dir=None)...所有测试模块必须可以从项目的顶层导入。如果起始目录不是顶级目录,则必须单独指定顶级目录...
我自己做了一些实验,似乎 whentop_level_dir是不同的start_dir并且没有设置,没有出错:所有测试都被发现并正确导入。
我想知道将top_level_dir参数传递给discover. 为什么它需要知道顶级目录是什么?我想它的所有工作都是 (i) 在 中找到测试包/模块start_dir,以及 (ii) 导入它们,不是吗?
或者,有人可以提供一个示例,说明没有top_level_dir导致测试发现至少部分失败吗?
我有一种情况,我想等到一个元素不再是 STALE,即直到一个元素连接到 DOM。以下等待选项以某种方式不起作用:
self.wait.until(EC.visibility_of_element_located((By.ID, "elementID")))
self.wait.until(EC.presence_of_element_located((By.ID, "elementID")))
Run Code Online (Sandbox Code Playgroud)
存在其相反的等待函数,它会等待元素变得陈旧,即:
self.wait.until(EC.staleness_of((By.ID, "elementID")))
Run Code Online (Sandbox Code Playgroud)
但我希望它等到元素不再过期,即直到它连接到 DOM。我怎样才能实现这个功能?
编辑:这里有一个解决方案:这里但我正在寻找任何其他更好的方法(如果有的话)。
我有代码和测试文件:
代码.py
class Code:
def do_something_inside(self, a, b, c):
return a-b-c
def do_something(self, b, c):
self.do_something_inside(30, b, c)
Run Code Online (Sandbox Code Playgroud)
测试文件
import unittest
import unittest.mock as mock
from code import Code
class TestStringMethods(unittest.TestCase):
def setUp(self):
self.code = Code()
def do_something_inside_stub(self, a, b, c):
return a+b+c
@mock.patch('code.Code.do_something_inside', new_callable=do_something_inside_stub)
def test_add(self):
self.assertEquals(self.code.do_something(10, 5), 45)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
我想使用 do_something_inside_stub 模拟 do_something_inside 方法,但执行失败:
E
======================================================================
ERROR: test_add (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/mock.py", line 1171, in patched
arg = patching.__enter__() …Run Code Online (Sandbox Code Playgroud) python ×10
python-unittest ×10
unit-testing ×5
pytest ×2
python-3.x ×2
automation ×1
dependencies ×1
html ×1
module ×1
pycharm ×1
python-3.6 ×1
python-mock ×1
selenium ×1
testing ×1