标签: pytest

如何限制单元测试的最长运行时间?

我目前正在运行一些单元测试,可能需要很长时间才能失败或无限期运行.在成功的测试运行中,它们将始终在一定的时间内完成.

是否有可能创建一个pytest单元测试,如果它在一定时间内没有完成就会失败?

python pytest

30
推荐指数
1
解决办法
9959
查看次数

有没有办法控制pytest-xdist如何并行运行测试?

我有以下目录布局:

runner.py
lib/
tests/
      testsuite1/
                 testsuite1.py
      testsuite2/
                 testsuite2.py
      testsuite3/
                 testsuite3.py
      testsuite4/
                 testsuite4.py
Run Code Online (Sandbox Code Playgroud)

testsuite*.py模块的格式如下:

import pytest 
class testsomething:
      def setup_class(self):
          ''' do some setup '''
          # Do some setup stuff here      
      def teardown_class(self):
          '''' do some teardown'''
          # Do some teardown stuff here

      def test1(self):
          # Do some test1 related stuff

      def test2(self):
          # Do some test2 related stuff

      ....
      ....
      ....
      def test40(self):
          # Do some test40 related stuff

if __name__=='__main()__'
   pytest.main(args=[os.path.abspath(__file__)])

我遇到的问题是我想并行执行'testsuites',即我希望testsuite1,testsuite2,testsuite3和testsuite4并行开始执行,但是测试套件中的单个测试需要连续执行.

当我使用py.test中的'xdist'插件并使用'py.test -n 4'启动测试时,py.test正在收集所有测试并随机地在4个工作者之间平衡测试.这导致在'testsuitex.py'模块中每次测试时都会执行'setup_class'方法(这违背了我的目的.我希望每个类只执行一次setup_class,然后在那里连续执行测试).

基本上我想要的执行看起来像是:

worker1: executes all …

python pytest xdist

29
推荐指数
3
解决办法
7253
查看次数

如何通过命令行在pytest中传递参数

我有一个代码,我需要传递像终端名称这样的参数.这是我的代码以及如何传递参数.我收到一个"文件未找到"的错误,我不明白.

我在终端尝试了这个命令:pytest <filename>.py -almonds 我应该把这个名字打印成"杏仁"

@pytest.mark.parametrize("name")
def print_name(name):
    print ("Displaying name: %s" % name)
Run Code Online (Sandbox Code Playgroud)

python pytest

29
推荐指数
6
解决办法
3万
查看次数

pytest参数化测试是否适用于基于单元测试类的测试?

我一直在尝试将参数化@pytest.mark.parametrize测试添加到基于类的单元测试中.

class SomethingTests(unittest.TestCase):
    @pytest.mark.parametrize(('one', 'two'), [
        (1, 2), (2, 3)])
    def test_default_values(self, one, two):
        assert one == (two + 1)
Run Code Online (Sandbox Code Playgroud)

但是参数化的东西没有起作用:

TypeError: test_default_values() takes exactly 3 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

我已经改用基于简单类的测试(没有单元测试).但是我想知道是否有人尝试过它并且有效.

python unit-testing pytest

28
推荐指数
2
解决办法
3747
查看次数

pytest:使用参数的笛卡尔积进行参数化测试

只是想知道,有没有(更多)优雅的方式参与笛卡尔积?这是我到目前为止所发现的:

numbers    = [1,2,3,4,5]
vowels     = ['a','e','i','o','u']
consonants = ['x','y','z']

cartesian = [elem for elem in itertools.product(*[numbers,vowels,consonants])]

@pytest.fixture(params=cartesian)
def someparams(request):
  return request.param

def test_something(someparams):
  pass
Run Code Online (Sandbox Code Playgroud)

至少我想在夹具功能中封装数字,元音,辅音和笛卡儿.

python pytest

27
推荐指数
3
解决办法
4792
查看次数

如何在virtualenv中使用pytest?

我将pytest安装到virtualenv并从该虚拟环境运行它,但它没有使用我在该虚拟环境中安装的软件包.相反,它使用主系统包.(使用"python -m unittest discover",我实际上可以使用正确的python和包运行我的测试,但我想使用py.test框架.)

是否有可能py.test实际上没有在virtualenv中运行pytest,我必须指定运行哪个pytest?

如何让py.test只使用我的virtualenv中的python和包?

另外,由于我的系统上有几个蟒蛇,我怎么知道哪个python pytest正在使用?它会在我的virtualenv中自动使用python,还是我必须以某种方式指定?

python virtualenv pytest

27
推荐指数
3
解决办法
1万
查看次数

Pytest:如果一个失败,如何跳过课堂上的其他测试?

我正在使用Jenkins,Python,Selenium2(webdriver)和Py.test框架为Web测试创建测试用例.

到目前为止,我正在组织以下结构的测试:

每个都是测试用例,每个test_方法都是一个测试步骤.

当一切正常时,这个设置很有效,但当一步崩溃时,其余的"测试步骤"变得疯狂.我可以借助于包含类(测试用例)中的失败teardown_class(),但是我正在研究如何改进它.

我需要的是以某种方式跳过(或xfail)test_一个类中的其余方法,如果其中一个失败,那么其余的测试用例不会运行并标记为FAILED(因为这将是误报)

谢谢!

更新:我不是在寻找或回答"这是不好的做法",因为这样称呼它是非常有争议的.(每个测试类都是独立的 - 这应该足够了).

更新2:在每个测试方法中放置"if"条件不是一个选项 - 是很多重复工作.我正在寻找的是(也许)有人知道如何使用类方法的钩子.

python automated-tests pytest selenium-webdriver

26
推荐指数
3
解决办法
2万
查看次数

Py.test没有名为*的模块

我有这样的文件夹结构

App
--App
  --app.py       
--Docs
--Tests
  --test_app.py
Run Code Online (Sandbox Code Playgroud)

在我test_app.py file,我有一行来导入我的应用程序模块.当我在根文件夹上运行py.test时,我得到关于没有名为app的模块的错误.我该如何配置?

python pytest

26
推荐指数
7
解决办法
3万
查看次数

当py.test测试失败时,PyCharm可以进入调试状态

运行测试时py.test,可以--pdb选择在失败时输入pdb.

在PyCharm中运行相同的测试时是否有类似的方法进入调试器?

python pytest pycharm

26
推荐指数
1
解决办法
3514
查看次数

如何使用 pytest 模拟请求?

我正在编写一些单元测试代码,我想模拟requests在我的函数中使用的模块:

import requests

def get_employee(id):
    resp = requests.get(f'{__BASE_URL}/employee/{id}')
    if resp.status_code == 404:
        return None

    return resp.json()
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码模拟它:

def test_get_employee(mocker):
    get_request_mock = mocker.patch.object(get_employee, "resp")
    print(get_request_mock)
    get_request_mock.status_code = 200
    get_request_mock.json.return_value = {'name': 'awesome-mock'}
    resp = get_employee('random-id')
    assert resp == {'name': 'awesome-mock'}
Run Code Online (Sandbox Code Playgroud)

如何使用 mocker 模拟请求?有可能吗?

python pytest

26
推荐指数
1
解决办法
2万
查看次数