小编Shi*_*a91的帖子

从同级目录导入模块以与 py.test 一起使用

我在将任何内容导入我打算使用 py.test 运行的测试文件时遇到问题。

我有一个项目结构如下:

/ProjectName
|
|-- /Title
|    |-- file1.py
|    |-- file2.py
|    |-- file3.py
|    |-- __init__.py
|
|-- /test
|    |-- test_file1.py
Run Code Online (Sandbox Code Playgroud)

我无法在test_file1.py文件中使用 pytest 获得任何导入语句,因此目前我只是尝试使用声明的变量file_1.py并在test_file1.py运行时将其打印出来。

file1.py 包含:

file1_variable = "Hello"
Run Code Online (Sandbox Code Playgroud)

test_file1.py 包含:

import sys
import os
sys.path.append(os.path.abspath('../Title'))
import file1

def test_something():
    assert file1.file1_variable == "Hello"

print(file1.file1_variable)
Run Code Online (Sandbox Code Playgroud)

我的测试文件中的导入语句取自/sf/answers/719104361/,它通过编辑 PYTHONPATH 变量来工作。这允许我运行test_file1.py脚本并成功执行print语句。

但是,尝试py.test/ProjectName目录运行会给我一个错误提示ImportError: No module named 'file1'

有什么方法可以让我更好地构建事物,以便 pytest 成为可能?我需要在我的__init__.py文件中添加一些东西吗?

python python-import importerror pytest python-3.x

5
推荐指数
1
解决办法
4788
查看次数

如何为2种不同的语言配置Vim?

我目前正在使用Vim for Python,并希望在我学习Ruby的同时开始使用它.

有没有办法配置vimrc文件,以便根据当前正在处理的文件类型应用不同的设置?

例如,我的vimrc当前设置为有4个空格的缩进,我想将它们作为Ruby文件的2个空格.另外,我希望在处理ruby文件时使用语法Ruby语法突出显示,并为Python文件突出显示Python语法.

我偶然发现了这个以定义制表符空格:

autocmd FileType python set tabstop=8|set shiftwidth=4|set expandtab
autocmd FileType ruby set tabstop=8|set shiftwidth=2|set expandtab
Run Code Online (Sandbox Code Playgroud)

语法高亮是否有类似的东西?

ruby vim file-type indentation vim-syntax-highlighting

0
推荐指数
1
解决办法
465
查看次数