簡體   English   中英

pytest 使用 Makefile 和 make 命令

[英]pytest using Makefile and make command

我是第一次學習測試驅動開發。 我沒有軟件開發經驗,但有一些腳本編寫經驗。

我一直在關注 LinuxAcademy Python 3 for Sys Admin 教程。

我創建了以下結構,

├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.rst
├── setup.py
├── src
│   └── pgbackup
│       ├── cli.py
│       └── __init__.py
└── tests
    └── test_cli.py

setup.py文件,

from setuptools import setup, find_packages
    with open('README.rst', 'r') as f:
        readme = f.read()
    setup(
        name='pgbackup',
        version='0.1.0',
        description='Database backups locally or to AWS S3.',
        long_description=readme,
        author='Keith Thompson',
        author_email='keith@linuxacademy.com',
        packages=find_packages('src'),
        package_dir={'': 'src'},
    )

Makefile文件,

.PHONY: install test

default: test

install:
        pipenv install --dev --skip-lock

test:
        PYTHONPATH=./src pytest

tests/test_cli.py文件,

import pytest
from pgbackup import cli

def test_helloworld():
    """
    JUST A HELLO WORLD TEST
    """
    assert cli.hello() == "helloworld"

src/pgbackup/cli.py文件,

def hello():
    return "helloworld"

我寫了helloworld作為我的第一個示例測試,它不是教程的一部分。 現在當我從項目根目錄運行make命令時,我的測試通過了,

========================================== test session starts ===========================================platform linux -- Python 3.6.6, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: /root/code/pgbackup, inifile:
collected 1 item

tests/test_cli.py .                                                                                [100%]

======================================== 1 passed in 0.04 seconds ========================================

我知道make命令將PYTHONPATH設置為./src pytest但我不知道它是如何運行實際測試的? 我知道它只是設置了一個搜索路徑來導入 python 模塊。

如果我嘗試從tests目錄運行pytest命令,我的測試失敗,

================================================= ERRORS =================================================___________________________________ ERROR collecting tests/test_cli.py ___________________________________ImportError while importing test module '/root/code/pgbackup/tests/test_cli.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_cli.py:2: in <module>
    from pgbackup import cli
E   ModuleNotFoundError: No module named 'pgbackup'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!======================================== 1 error in 0.35 seconds =========================================

如果我從src目錄運行測試,它不會運行任何東西,

====================================== no tests ran in 0.01 seconds ======================================

有人可以解釋一下如何運行make運行測試,因為Makefile只是設置PYTHONPATH變量?

有人可以解釋一下如何運行 make 運行測試,因為 Makefile 只是設置 PYTHONPATH 變量?

這不僅僅是設置變量。 pytest 正在此處運行,因為您當前的測試行:

PYTHONPATH=./src pytest

相當於:

export PYTHONPATH=./src; pytest

在此處檢查第三個示例: Bash 變量賦值中的命令未找到錯誤以獲取解釋

您可能希望將其更改為相應的外觀,

如果我嘗試從測試目錄運行 pytest 命令,我的測試失敗,

至於從不同的文件夾運行pytest,確實會有不同的結果。 因為 pytest 使用自動檢測測試( test文件夾中的所有文件,或名為test_*當前文件夾)。 所以如果你在src里面運行它,里面沒有測試。

如果我嘗試從測試目錄運行 pytest 命令,我的測試如果失敗

pgbackup 似乎是從當前目錄導入的,所以當你移動到 'tests' 文件夾中時,它不會被找到。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM