你如何从pycharm执行nosetest来运行所有的单元测试?
我知道pycharm支持python的unittest和py.test,并且他们将在pycharm 1.1中正确支持nosetests,但我想知道是否有解决方法.
我目前尝试使用模拟库在python中编写一些基本的鼻子单元测试.
在完成了一些基本的例子后,我现在尝试使用nosetests --with-coverage
,现在我有了模拟包,我试图'模拟掉'的包显示在覆盖率报告中.是否有可能排除这些?
这是我要测试的类:
from imaplib import IMAP4
class ImapProxy:
def __init__(self, host):
self._client = IMAP4(host)
Run Code Online (Sandbox Code Playgroud)
和测试用例:来自模拟导入补丁
from ImapProxy import ImapProxy
class TestImap:
def test_connect(self):
with patch('ImapProxy.IMAP4') as imapMock:
proxy = ImapProxy("testhost")
imapMock.assert_called_once_with("testhost")
Run Code Online (Sandbox Code Playgroud)
我现在得到以下输出 nosetests --with-coverage
.
Name Stmts Miss Cover Missing
------------------------------------------
ImapProxy 4 0 100%
imaplib 675 675 0% 23-1519
mock 1240 810 35% [ a lot of lines]
Run Code Online (Sandbox Code Playgroud)
有没有办法排除模拟包和imaplib包,而不必手动将除了那些包之外的所有包列入白名单--cover-package=PACKAGE
感谢Ned Batchelder,我现在知道.coveragerc文件了,谢谢!
我创建了一个包含以下内容的.coveragerc文件:
[report]
omit = *mock*
Run Code Online (Sandbox Code Playgroud)
现在我在覆盖率报告中的模拟输出是:
mock 1240 1240 0% 16-2356 …
Run Code Online (Sandbox Code Playgroud) 我正试图在我的计算机上安装鼻子来学习"学习Python困难之路"教程,但似乎无法让它工作.我正在使用pip安装:
$ pip install nose
Run Code Online (Sandbox Code Playgroud)
我回来了:
Requirement already satisfied (use --upgrade to upgrade): nose in /usr/local/lib/python2.7/site-packages
Cleaning up...
Run Code Online (Sandbox Code Playgroud)
但是,当我运行命令nosetests时,我得到:
-bash: nosetests: command not found
Run Code Online (Sandbox Code Playgroud)
我认为我的PATH有问题,老实说我不知道.任何帮助将不胜感激!
我尝试使用nosetests
❯nosetests'/ pathTo/test'
但它python 2.7
用于我的测试:
sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)
所以有些人失败了,因为他们写的是python 3.3
.
我解决它并安装虚拟环境:
pyvenv-3.3 py3env
Run Code Online (Sandbox Code Playgroud)
激活它:
source ~/py3env/bin/activate
Run Code Online (Sandbox Code Playgroud)
检查虚拟环境中的python virsion:
? python --version ?
Python 3.3.3
(py3env)
Run Code Online (Sandbox Code Playgroud)
好.但是nosetest仍然python2.7
在虚拟环境中使用:
sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)
所以我的测试失败了. 如何让鼻子使用python3?
我有一些Selenium Webdriver GUI测试,可以在我们的Trac/Bitten构建环境中的每次签入中运行.由于各种愚蠢的原因,这些是脆弱的并且重新运行失败的测试始终有效(除非它没有,然后我有一个实际上失败的测试).
如何重新运行这些失败的测试?GUI测试运行需要10-15分钟,因此重新运行所有这些都是一件痛苦的事.
这是构建步骤:
<step id="guitest" description="Run gui tests">
<sh:exec executable="gui_tests.sh" />
</step>
Run Code Online (Sandbox Code Playgroud)
和gui_tests.sh
# Setup environment stuff deleted
nosetests python/*Tests.py -v
Run Code Online (Sandbox Code Playgroud)
我们正在努力使GUI测试更加健壮,但我的理解是,这是GUI测试的生命.
我的项目文件夹(是的 - 我知道这是最好的做法)是这样的:
.
??? app.py
??? otherscript.py
??? tests/
??? tools/
??? __init__.py
??? toolfile.py
Run Code Online (Sandbox Code Playgroud)
我需要nose --with-coverage
测试.py
主文件夹,tools
文件夹中的脚本并排除tests
文件夹(虽然我真的不在乎排除它)
当我运行基本
nose --with-coverage
Run Code Online (Sandbox Code Playgroud)
我得到了所有安装的依赖项和库(烧瓶,请求等)的报道
我跑的时候
nose --with-coverage --cover-package=folder name (or . or ./)
Run Code Online (Sandbox Code Playgroud)
我得到了测试文件夹的覆盖范围.该tools/__init__.py
文件,app.py
但不包括其余脚本:
> (opentaba-server) D:\username\pathto\opentaba-server>nosetests --with-coverage -- cover-package=./ ... Name
> Stmts Miss Cover Missing
> ----------------------------------------------------------------------- Tests\functional_tests\test_return_json 26 0 100%
> Tests\unit_test\test_createdb 0 0 100%
> Tests\unit_test\test_scrape 0 0 100% app
> 63 15 76% 22, 24, …
Run Code Online (Sandbox Code Playgroud) 我的Python代码中有一些相对复杂的集成测试.我用自定义装饰器大大简化了它们,我对结果非常满意.这是我的装饰器看起来像一个简单的例子:
def specialTest(fn):
def wrapTest(self):
#do some some important stuff
pass
return wrapTest
Run Code Online (Sandbox Code Playgroud)
这是测试的样子:
class Test_special_stuff(unittest.TestCase):
@specialTest
def test_something_special(self):
pass
Run Code Online (Sandbox Code Playgroud)
这很好用,并且由PyCharm的测试运行器执行没有问题.但是,当我使用Nose从命令行运行测试时,它会使用@specialTest装饰器跳过任何测试.我试图将装饰器命名为testSpecial,因此它匹配默认规则,但我的FN参数不会被传递.
我如何让Nose执行这些测试方法并按照预期处理装饰器?
感谢madjar,我通过重构我的代码看起来像这样,使用functools.wraps并更改包装器的名称:
from functools import wraps
def specialTest(fn):
@wraps(fn)
def test_wrapper(self,*args,**kwargs):
#do some some important stuff
pass
return test_wrapper
class Test_special_stuff(unittest.TestCase):
@specialTest
def test_something_special(self):
pass
Run Code Online (Sandbox Code Playgroud) Python unittest使用nosetests来试验Python Class和Module fixture,在我的测试中进行最小化设置.
我面临的问题是,我不确定如何使用我的测试中setupUpModule
的setUpClass
函数和函数中定义的任何变量(例如: - test_1
).
这是我用来尝试的:
import unittest
def setUpModule():
a = "Setup Module variable"
print "Setup Module"
def tearDownModule():
print "Closing Module"
class TrialTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
print a #<======
b = "Setup Class variable"
@classmethod
def tearDownClass(cls):
print "Closing Setup Class"
def test_1(self):
print "in test 1"
print a #<======
print b #<======
def test_2(self):
print "in test 2"
def test_3(self):
print "in test 3"
def test_4(self): …
Run Code Online (Sandbox Code Playgroud) 我nosetests test.py
用来运行单元测试:
import unittest
import logging
class Test(unittest.TestCase):
def test_pass(self):
logging.getLogger('do_not_want').info('HIDE THIS')
logging.getLogger('test').info('TEST PASS')
self.assertEqual(True, True)
def test_fail(self):
logging.getLogger('do_not_want').info('HIDE THIS')
logging.getLogger('test').info('TEST FAIL')
self.assertEqual(True, False)
Run Code Online (Sandbox Code Playgroud)
当测试失败时,它会打印出所有日志记录信息.我可以使用--logging-filter
只提取一些记录器:
nosetests test.py --verbosity=2 --logging-filter=test
test_fail (test.Test) ... FAIL
test_pass (test.Test) ... ok
======================================================================
FAIL: test_fail (test.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File ".../test.py", line 14, in test_fail
self.assertEqual(True, False)
AssertionError: True != False
-------------------- >> begin captured logging << --------------------
test: INFO: TEST FAIL
--------------------- >> end captured logging << …
Run Code Online (Sandbox Code Playgroud) 我需要能够通过在Linux shell中键入一行来在当前目录中运行所有测试.在某些目录中,这很好用.但在其他情况下,当我输入"nosetests"时,没有进行任何测试.如果我单独调用它们,测试将运行,但我需要它们全部自动运行.这是一个不起作用的目录:
/extwebserver
__init__.py
test_Detection.py
test_Filesystem.py
test_Hardware.py
...
Run Code Online (Sandbox Code Playgroud)
当我在父目录中运行"nosetests"时,将运行某个子目录中的所有测试,但不会运行/ extwebserver或其他子目录或父目录本身的测试.
编辑 这是输出:
matthew@Matthew-Laptop:~/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing$ nosetests -vv --collect-only
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/baseTestCase.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/run.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Detection.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Filesystem.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Hardware.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Mode.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_System.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_View.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Webserver.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/mocks/bottle.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/utils/test_timestamps.py is …
Run Code Online (Sandbox Code Playgroud) nose ×10
python ×9
nosetests ×3
unit-testing ×3
decorator ×1
ignore ×1
logging ×1
mocking ×1
oop ×1
pycharm ×1
python-2.7 ×1
python-mock ×1