如何在不执行py.test中的父脚本的情况下运行测试?目前我从目录Project_dir运行命令pytest -v
,这将运行 script.py 并最终进行测试。理想情况下,只需要运行测试。这是我当前的目录设置:
Project_dir
script.py
test
test_script.py
script.py
def add(a,b):
added = a + b
return added
x = add(1,3)
print x, 'yup'
test_script.py
import script
def test_add():
assert script.add(3,4) == 7
工作目录: Project_dir
命令: pytest -vs
========================================== test session starts ==========================================
platform darwin -- Python 2.7.10, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /usr/bin/python
cachedir: .cache
rootdir: **, inifile:
plugins: cov-2.4.0
collecting 0 items
Sum is 4
collected 1 items
test/test_script.py::test_add PASSED
======================================= 1 passed in 0.00 seconds ========================================
Sum is 4
显示 script.py 完全执行而不仅仅是测试。当 script.py 作为模块导入时,会发生这种情况。但我一直认为必须有办法避免这种情况,直接执行测试。
答案 0 :(得分:2)
在Python中导入模块会执行其中的所有顶级代码。
您应该将代码放在if __name__ == '__main__':
中,以便它只在您运行脚本时执行(但在导入时不会执行) - 请参阅例如this answer了解详情。
答案 1 :(得分:0)
尝试Ignore paths during test collection
rs = RenderScript.create(this);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
// you don´t need Type.Builder objects , with cameraPreviewWidth and cameraPreviewHeight do:
int yuvDatalength = cameraPreviewWidth*cameraPreviewHeight*3/2; // this is 12 bit per pixel
aIn = Allocation.createSized(rs, Element.U8(rs), yuvDatalength);
// of course you will need the Bitmap
bmpout = Bitmap.createBitmap(cameraPreviewWidth, cameraPreviewHeight, Bitmap.Config.ARGB_8888);
// create output allocation from bitmap
aOut = Allocation.createFromBitmap(rs,bmpout); // this simple !
// set the script´s in-allocation, this has to be done only once
yuvToRgbIntrinsic.setInput(aIn);
或者在目录上运行pytest:
pytest -v --ignore=script.py
答案 2 :(得分:0)
如何运行pytest test/
?
只是为了说清楚:
https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests
pytest somepath # run all tests below somepath