我正在尝试使用PyCharm进行单元测试(使用unittest),并且能够使其工作:测试运行器很好地显示测试用例列表和嵌套测试函数.
但是,一旦发现测试,我找不到任何方法(重新)运行特定的测试功能:唯一可用的按钮将运行整个测试列表,右键单击一个测试功能不会显示任何有意义的为此目的的行动.
可以想象,当目的是调试单个测试时,可能需要很长时间.
怎么做到这一点?例如,它可以在Visual Studio中使用,并且看起来像是一个基本功能,因此我假设我必须遗漏一些东西.
所以我想做这个代码Kata练习.我想在单独的文件中用tdd实现kata:
算法:
# stringcalculator.py
def Add(string):
return 1
Run Code Online (Sandbox Code Playgroud)
和测试:
# stringcalculator.spec.py
from stringcalculator import Add
import unittest
class TestStringCalculator(unittest.TestCase):
def add_returns_zero_for_emptyString(self):
self.assertEqual(Add(' '), 0)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
运行测试文件时,我得到:
Ran 0 tests in 0.000s
OK
Run Code Online (Sandbox Code Playgroud)
但它应该返回一个失败的测试.我在这里想念什么?
我想确保我的代码中的某个条件导致将日志消息写入django日志.我如何使用Django单元测试框架?
有没有我可以检查已记录消息的地方,类似于我如何查看已发送的电子邮件?我的单元测试延伸django.test.TestCase.
我正在编写一个Pythonic工具,用于验证某个系统的正确性.每个验证都是用Python编写的unittest,报告如下:
test_exclude_list_not_empty (__main__.TestRepoLists)
Assert the the exclude list is not empty ... ok
test_include_list_not_empty (__main__.TestRepoLists)
Assert the the include list is not empty ... ok
test_repo_list_not_empty (__main__.TestRepoLists)
Assert the the repo list is not empty ... ok
Run Code Online (Sandbox Code Playgroud)
在我看来,这种格式很难阅读,特别是对于非Python主义者.是否有任何报告生成器可以以漂亮的表格形式生成报告,例如:
+----------------------------------------------------------------+-----------+
| Test | Status |
+----------------------------------------------------------------+-----------+
| Assert the the exclude list is not empty | OK |
| Assert the the include list is not empty | OK |
| Assert the the repo list is not empty …Run Code Online (Sandbox Code Playgroud) 我正在使用Flask-Testing进行Flask集成测试.我有一个表格,上面有一个徽标文件,我正在尝试编写测试但是我一直收到错误说:TypeError: 'str' does not support the buffer interface.
我正在使用Python 3.我找到的最接近的答案是这个,但它对我不起作用.
这是我的许多尝试之一:
def test_edit_logo(self):
"""Test can upload logo."""
data = {'name': 'this is a name', 'age': 12}
data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg')
self.login()
response = self.client.post(
url_for('items.save'), data=data, follow_redirects=True)
})
self.assertIn(b'Your item has been saved.', response.data)
advert = Advert.query.get(1)
self.assertIsNotNone(item.logo)
Run Code Online (Sandbox Code Playgroud)
如何在Flask中测试文件上传?
有关PyUnit入门的最佳教程是什么?
(在Google上,我倾向于发现大多数支离破碎的提示和技巧,并且在指南方面并不多)
我是Python的新手,并尝试在Ruby中经常做一些事情.即,迭代一组索引,使用它们作为函数的参数并将其结果与夹具输出数组进行比较.
所以我像往常一样在Ruby中编写它,但这导致只有一个测试用例.
def test_output(self):
for i in range(1,11):
....
self.assertEqual(fn(i),output[i])
Run Code Online (Sandbox Code Playgroud)
我正在尝试为该范围内的每个项目进行测试.我怎样才能做到这一点?
我正在修改一些代码以兼容Python 2和Python 3,但在单元测试输出中发现了警告.
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:601:
ResourceWarning: unclosed socket.socket fd=4,
family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6,
laddr=('1.1.2.3', 65087), raddr=('5.8.13.21', 8080)
Run Code Online (Sandbox Code Playgroud)
我可以忽略警告或完全过滤它.如果是我的服务,我可以connection: close在我的响应(链接)中设置标题.
这是一个展示警告的示例Python 3.6.1:
app.py
import requests
class Service(object):
def __init__(self):
self.session = requests.Session()
def get_info(self):
uri = 'http://api.stackexchange.com/2.2/info?site=stackoverflow'
response = self.session.get(uri)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
def __del__(self):
self.session.close()
if __name__ == '__main__':
service = Service()
print(service.get_info())
Run Code Online (Sandbox Code Playgroud)
test.py
import unittest
class TestService(unittest.TestCase):
def test_growing(self): …Run Code Online (Sandbox Code Playgroud) 我有PYCharm 3.0.1的问题我无法运行基本的单元测试.
这是我的代码:
import unittest from MysqlServer import MysqlServer
class MysqlServerTest(unittest.TestCase):
def setUp(self):
self.mysqlServer = MysqlServer("ip", "username", "password", "db", port)
def test_canConnect(self):
self.mysqlServer.connect()
self.fail()
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
这是pycharm给我的所有东西
无法将测试报告者附加到测试框架或测试框架意外退出
也说
AttributeError: class TestLoader has no attribute '__init__'
Run Code Online (Sandbox Code Playgroud)
事件日志:
2:14:28 PM Empty test suite
Run Code Online (Sandbox Code Playgroud)
问题是我手动运行python文件(使用pycharm,作为脚本)
----------------------------------------------------------------------
Ran 1 tests in 0.019s
FAILED (failures=1)
Run Code Online (Sandbox Code Playgroud)
这是正常的我让测试失败了.我对这里发生的事情有点了解更多信息:设置 - > Python集成工具 - >包需求文件:/ src/test默认测试运行器:Unittests pyunit 1.4.1已安装
谢谢你的帮助.
编辑:unitests.py的基本用法也是如此
import unittest
class IntegerArithmenticTestCase(unittest.TestCase):
def testAdd(self): ## test method names begin 'test*'
self.assertEquals((1 + 2), …Run Code Online (Sandbox Code Playgroud) 为了避免循环导入,我被迫定义一个看起来像这样的函数:
# do_something.py
def do_it():
from .helpers import do_it_helper
# do stuff
Run Code Online (Sandbox Code Playgroud)
现在我希望能够通过do_it_helper修补来测试这个功能.如果导入是顶级导入,
class Test_do_it(unittest.TestCase):
def test_do_it(self):
with patch('do_something.do_it_helper') as helper_mock:
helper_mock.return_value = 12
# test things
Run Code Online (Sandbox Code Playgroud)
会工作得很好.但是,上面的代码给了我:
AttributeError: <module 'do_something'> does not have the attribute 'do_it_helper'
Run Code Online (Sandbox Code Playgroud)
一时兴起,我也尝试将补丁语句更改为:
with patch('do_something.do_it.do_it_helper') as helper_mock:
Run Code Online (Sandbox Code Playgroud)
但这产生了类似的错误.有没有办法模拟这个函数,因为我被迫在它使用的函数中导入它?
python-unittest ×10
python ×9
unit-testing ×6
testing ×4
pycharm ×2
python-3.x ×2
django ×1
flask ×1
mocking ×1
sockets ×1