回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我可以按照<a href="https://www.odoo.com/forum/how-to/developers-13/how-to-run-tests-526" rel="noreferrer">here</a>所述运行openerpv7附加组件的单元测试。在</p>
<p>在<strong>PyCharm</strong>中,我在<em>运行/调试配置</em>中添加了一个Python配置,如下所示:</p>
<p><strong>脚本:</strong></p>
<pre><code>/home/juliocesar/work/projects/my_project/openerp-server
</code></pre>
<p><strong>脚本参数:</strong></p>
^{pr2}$
<p>它成功运行,但以文本日志格式显示标准输出,如下所示:</p>
<pre><code>2015-04-24 13:47:55,101 12340 TEST my_project openerp.modules.module: module my_addon: executing 1 `fast_suite` and/or `checks` sub-modules
2015-04-24 13:47:55,101 12340 TEST my_project openerp.modules.module: test_change_old_received_to_contingency (openerp.addons.my_addon.tests.test_my_addon.TestMyItems)
2015-04-24 13:47:55,101 12340 TEST my_project openerp.modules.module: ` Test patch to change old received status to contingency.
2015-04-24 13:47:55,110 12340 TEST my_project openerp.modules.module: Ran 1 tests in 0.006s
2015-04-24 13:47:55,110 12340 TEST my_project openerp.modules.module: OK
</code></pre>
<p>其中它显示了运行我在项目<code>my_project</code>,在<code>/home/juliocesar/work/projects/my_project/openerp/addons/my_addon/tests/test_my_addon.py</code>中的附加组件<code>my_addon</code>中创建的以下测试的结果:</p>
<pre><code>from openerp.tests.common import TransactionCase
import unittest2
class TestMyItems(TransactionCase):
def test_change_old_received_to_contingency(self):
"""Test patch to change old received status to contingency."""
self.assertTrue(True)
if __name__ == '__main__':
unittest2.main()
</code></pre>
<p>我想要的是使用<em>Python tests->;Unittest</em>配置来显示带有红/绿图标的测试输出和测试结果的PyCharm界面。在</p>
<p><img src="https://i.stack.imgur.com/gURsQ.png" alt="PyCharm unittest config"/></p>
<p>Unittest配置需要测试所在的脚本文件,如果我指定该文件,PyCharm会查找文件中的所有测试,但会出错,因为没有配置数据库(以及其他参数,如<code>openerp-server</code>脚本和上面指定的用于运行OpenERP测试的rest参数):</p>
<p><img src="https://i.stack.imgur.com/14GEf.png" alt="PyCharm unittest results"/></p>
<p>这是运行此配置的结果:</p>
<pre><code>/usr/bin/python2.7 /home/juliocesar/apps/pycharm/helpers/pycharm/utrunner.py /home/juliocesar/work/projects/my_project/openerp/addons/my_addon/tests/ false
Testing started at 09:38 AM ...
No handlers could be found for logger "openerp.sql_db"
Process finished with exit code 0
Error
Traceback (most recent call last):
File "/home/juliocesar/work/projects/my_project/openerp/tests/common.py", line 94, in setUp
TransactionCase.cr = self.cursor()
File "/home/juliocesar/work/projects/my_project/openerp/tests/common.py", line 55, in cursor
return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
File "/home/juliocesar/work/projects/my_project/openerp/modules/registry.py", line 193, in get
update_module)
File "/home/juliocesar/work/projects/my_project/openerp/modules/registry.py", line 209, in new
registry = Registry(db_name)
File "/home/juliocesar/work/projects/my_project/openerp/modules/registry.py", line 76, in __init__
cr = self.db.cursor()
File "/home/juliocesar/work/projects/my_project/openerp/sql_db.py", line 484, in cursor
return Cursor(self._pool, self.dbname, serialized=serialized)
File "/home/juliocesar/work/projects/my_project/openerp/sql_db.py", line 182, in __init__
self._cnx = pool.borrow(dsn(dbname))
File "/home/juliocesar/work/projects/my_project/openerp/sql_db.py", line 377, in _locked
return fun(self, *args, **kwargs)
File "/home/juliocesar/work/projects/my_project/openerp/sql_db.py", line 440, in borrow
result = psycopg2.connect(dsn=dsn, connection_factory=PsycoConnection)
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
OperationalError: FATAL: database "False" does not exist
</code></pre>
<p>那么,如何使用PyCharm测试配置来指定运行openerpv7 unittest所需的参数呢?在</p>
<p>我使用了PyCharm 4.0.6构建版本PY-139.1659,但它在PyCharm 5中也不起作用。在</p>