Python - 除非指定,否则使用 pytest 跳过测试 [英] Python - Using pytest to skip test unless specified

查看:68
本文介绍了Python - 除非指定,否则使用 pytest 跳过测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pytest 来测试将数据推送到数据库的网络抓取工具.该类仅拉取 html 并将 html 推送到数据库以供稍后解析.我的大多数测试使用虚拟数据来表示 html.

I have am using pytest to test a web scraper that pushes the data to a database. The class only pulls the html and pushes the html to a database to be parsed later. Most of my tests use dummy data to represent the html.

我想做一个测试,从网站上抓取一个网页,但我希望测试自动关闭,除非指定.类似的情况可能是,如果您不想总是运行一个昂贵或耗时的测试.

I want to do a test where a webpage from the website is scraped but I want the test to be automatically turned off unless specified. A similar scenario could be if you have an expensive or time consuming test that you do not want to always run.

除非我让 pytest 运行所有被抑制的测试,否则我期待某种抑制测试的标记,但我在文档中没有看到.

I am expecting some kind of marker that suppresses a test unless I give pytest to run all suppressed tests, but I do not see that in the documentation.

  • 我目前正在使用跳过标记并将其注释掉.
  • 尝试使用 skipif 标记和使用此命令从命令提示符 pytest test_file.py 1 和测试文件中的以下代码为 python 脚本提供参数.问题是,当我尝试为我的 test_file 提供参数时,pytest 期望它是另一个文件名,因此我收到错误消息0.00 秒内没有运行测试,错误:找不到文件:1"

  • I am currently using the skip marker and comment it out.
  • Tried to use the skipif marker and and give arguments to python script using this command from command prompt pytest test_file.py 1 and the following code below in the test file. The problem is that when I try to provide an argument to my test_file, pytest is expecting that to be another file name so I get an error "no tests run in 0.00 seconds, ERROR: file not found: 1"

if len(sys.argv) == 1:
  RUN_ALL_TESTS = False
else:
  RUN_ALL_TESTS = True
...
# other tests
...

@pytest.mark.skipif(RUN_ALL_TESTS)
def test_scrape_website():
  ...

  • 我也许可以将测试视为夹具并使用 @pytest.fixture(autouse=False),但不确定如何覆盖 autouse 变量

  • I might be able to treat the test as a fixture and use @pytest.fixture(autouse=False), not sure how to override the autouse variable though

    How to使用外部装置跳过 pytest? 但这个解决方案似乎比我需要的更复杂.

    A similar solution was stated in How to skip a pytest using an external fixture? but this solutions seems more complicated than what I need.

    推荐答案

    有几种方法可以解决这个问题,但我将介绍我在 Python 基线中看到的两种常见方法.

    There's a couple ways to handle this, but I'll go over two common approaches I've seen in Python baselines.

    1) 通过将可选"测试放在另一个目录中来分隔测试.

    不确定你的项目布局是什么样的,但你可以这样做(只有测试目录很重要,其余的只是一个玩具示例布局):

    Not sure what your project layout looks like, but you can do something like this (only the test directory is important, the rest is just a toy example layout):

    README.md
    setup.py
    requirements.txt
    test/
        unit/
            test_something.py
            test_something_else.py
        integration/
            test_optional.py
    application/
        __init__.py
        some_module.py
    

    然后,当您调用 pytest 时,如果您想运行单元测试(即仅 test_something*.py 文件),或者 pytest test/integration 如果您想只是运行集成测试(即只有 test_optional.py),或者 pytest test 如果你想运行所有测试.因此,默认情况下,您只需运行 pytest test/unit.

    Then, when you invoke pytest, you invoke it by doing pytest test/unit if you want to run just the unit tests (i.e. only test_something*.py files), or pytest test/integration if you want to run just the integration tests (i.e. only test_optional.py), or pytest test if you want to run all the tests. So, by default, you can just run pytest test/unit.

    我建议将这些调用封装在某种脚本中.我更喜欢 make 因为它对于这种类型的包装功能强大.然后你可以说make test,它只运行你的默认(快速)测试套件,或者make test_all,它会运行所有的测试(可能会也可能不会)慢).

    I recommend wrapping these calls in some sort of script. I prefer make since it is powerful for this type of wrapping. Then you can say make test and it just runs your default (fast) test suite, or make test_all, and it'll run all the tests (which may or may not be slow).

    您可以使用的 Makefile 示例:

    Example Makefile you could wrap with:

    .PHONY: all clean install test test_int test_all uninstall
    
    all: install
    
    clean:
        rm -rf build
        rm -rf dist
        rm -rf *.egg-info
    
    install:
        python setup.py install
    
    test: install
        pytest -v -s test/unit
    
    test_int: install
        pytest -v -s test/integration
    
    test_all: install
        pytest -v -s test
    
    uninstall:
        pip uninstall app_name
    

    2) 使用 @pytest.mark.skipif 装饰器明智地标记您的测试,但使用环境变量作为触发器

    2) Mark your tests judiciously with the @pytest.mark.skipif decorator, but use an environment variable as the trigger

    我不太喜欢这个解决方案,对我来说感觉有点随意(很难判断在任何给定的 pytest 运行中正在运行哪一组测试).但是,您可以做的是定义一个环境变量,然后将该环境变量绑定到模块中以检测是否要运行所有测试.环境变量依赖于 shell,但我会假设你有一个 bash 环境,因为这是一个流行的 shell.

    I don't like this solution as much, it feels a bit haphazard to me (it's hard to tell which set of tests are being run on any give pytest run). However, what you can do is define an environment variable and then rope that environment variable into the module to detect if you want to run all your tests. Environment variables are shell dependent, but I'll pretend you have a bash environment since that's a popular shell.

    您可以为快速单元测试执行 export TEST_LEVEL="unit"(因此这将是您的默认设置),或者 export TEST_LEVEL="all" 为所有您的测试.然后在您的测试文件中,您可以像这样执行您最初尝试执行的操作:

    You could do export TEST_LEVEL="unit" for just fast unit tests (so this would be your default), or export TEST_LEVEL="all" for all your tests. Then in your test files, you can do what you were originally trying to do like this:

    import os
    
    ...
    
    @pytest.mark.skipif(os.environ["TEST_LEVEL"] == "unit")
    def test_scrape_website():
      ...
    

    注意: 将测试级别命名为单元"和集成"是无关紧要的.您可以随意命名它们.您还可以有许多级别(例如夜间测试或性能测试).

    Note: Naming the test levels "unit" and "integration" is irrelevant. You can name them whatever you want. You can also have many many levels (like maybe nightly tests or performance tests).

    此外,我认为选项 1 是最好的方法,因为它不仅明确允许分离测试,而且还可以为测试的含义和表示增加语义和清晰度.但软件中没有一刀切",您必须根据自己的具体情况决定喜欢哪种方法.

    Also, I think option 1 is the best way to go, since it not only clearly allows separation of testing, but it can also add semantics and clarity to what the tests mean and represent. But there is no "one size fits all" in software, you'll have to decide what approach you like based on your particular circumstances.

    HTH!

    这篇关于Python - 除非指定,否则使用 pytest 跳过测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆