关于pycharm的debugger配置问题(包含启用py.test测试)

本文介绍了如何在PyCharm中集成pytest进行高效测试的方法。通过调整默认测试运行器为pytest,并正确设置测试目录和文件名,使得测试更加便捷。此外,还提供了另一种通过编辑配置来实现相同目的的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天才发现了一个刷新三观的问题。那就是 pycharm的底层集成的debugger功能其实是依赖于几个测试框架。。

现在由于要写基于pytest的测试,但是如果到zsh里面去跑命令,明显是效率很低下的一种方式。所以我半个晚上都在寻找如何把这玩意儿集成到pycharm里面

但是就算是使用pip install 安装了pytest_pycharm-0.3.0.dist-info模块 也没有找到可以使用的办法

各种查了半天,官方文档也查了都没有找到在哪儿。。

 

最后还是万能的stackoverflow http://stackoverflow.com/questions/6397063/how-do-i-configure-pycharm-to-run-py-test-tests

解决了这个问题。

 

大致是在参数配置里面的tool下面 有个 Python Intergrated Tools 的地方找到了 default test runner这个选项才发现。

原来pycharm的测试都是基于这些测试框架的

默认是系统自带的unittests测试框架。

在这里把这个选项改成pytest就可以了。这里注意经过的我测试 貌似只有在一级目录下才有效,什么意思呢:

就是将package requirements file 的路径设置到一个路径下,然后该路径下要有tests/test文件夹,然后该文件夹上面装测试用例就可以了test_开头的函数都可以被测试到。

如果是类的话类必须以Test开头 然后下面需要测试的函数使用test_开头才能被正常检查到并且被执行测试。

更详细的可以参照这里,pytest查找测试的规则:http://docs.pytest.org/en/latest/goodpractices.html#test-discovery

 

另外还有一种办法也可以设置

在run的 edit configurations 里面也可以手动添加。点击左上角的➕号 然后填写上target的目标地址 然后下面的working 的directory同样填写上 注意这里要选的是pytest。同样可以解决这个问题!希望能帮到大家。

就是普通的python文件运行报错:/usr/bin/python3 /Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 64866 --file /Users/shenxin/Desktop/176All/project/jiaoben/scriptproject/great/takealot_review250609amz/takealot_review_20250609amz.py AmazonCategories_20241018.csv _tal AmazonCategories_20241018_1.csv > outputamz.log 2>&1 & Connected to pydev debugger (build 223.8836.43) /Users/shenxin/Desktop/176All/project/jiaoben/scriptproject/great/takealot_review250609amz/takealot_review_20250609amz.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp /Library/Python/3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn( Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/Library/Python/3.9/site-packages/emoji/__init__.py", line 1, in <module> from .models import Emoji as emoji_class File "/Library/Python/3.9/site-packages/emoji/models.py", line 14, in <module> from . import settings File "/Library/Python/3.9/site-packages/emoji/settings.py", line 5, in <module> EMOJI_IMG_TAG = getattr(settings, 'EMOJI_IMG_TAG', ( File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 79, in __getattr__ self._setup(name) File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 60, in _setup raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting EMOJI_IMG_TAG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. python-BaseException Traceback (most recent call last): File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_xml.py", line 161, in _get_type def _get_type(self, o, type_object, type_name): File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/utils/functional.py", line 254, in inner def inner(self, *args): File "/Users/shenxin/Library/Python/3.9/lib/python/site-packages/django/conf/__init__.py", line 51, in _setup def _setup(self, name=None): django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. Process finished with exit code 1
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值