lundi 22 juin 2015

Unexpected behaviour with pytest fixture

I've this code

fixture:

import pytest

from aiorest_ws.command_line import CommandLine


@pytest.fixture(scope="function")
def command_line():
    cmd = CommandLine()
    cmd.define('-ip', default='127.0.0.1', help='used ip', type=str)
    cmd.define('-port', default=8080, help='available port', type=int)
    return cmd

tests:

from fixtures.command_line import command_line as cmd  # noqa

def test_define(cmd):
    assert len(cmd.options._option_string_actions.keys()) == 4

def test_define_with_user_arguments(cmd):
    cmd.define('-arg1', default='127.0.0.1',
                        help='used ip for server', type=str)
    cmd.define('-arg2', default=8080,
                        help='listened on server', type=int)
    assert len(cmd.options._option_string_actions.keys()) == 6

def test_define_with_user_arguments_and_options(cmd):
    cmd.define('-arg1', default='127.0.0.1',
                        help='used ip for server', type=str)
    cmd.define('-arg2', default=8080,
                        help='listened on server', type=int)
    cmd.define('--opt1', help='arg #1', type=int)
    cmd.define('--opt2', help='arg #2', type=str)
    assert len(cmd.options._option_string_actions.keys()) == 8

def test_parse_command_line(cmd):
    res = cmd.parse_command_line()
    assert res.ip == '127.0.0.1'
    assert res.port == 8080

Which failing with this errors:

______________________________________________ test_define_with_user_arguments ______________________________________________
Traceback (most recent call last):
  File "/Users/savicvalera/code/rest-ws/project/tests/test_command_line.py", line 17, in test_define_with_user_arguments
    cmd = command_line()
  File "/Users/savicvalera/code/rest-ws/project/tests/test_command_line.py", line 6, in command_line
    cmd.define('-ip', default='127.0.0.1', help='used ip', type=str)
  File "/Users/savicvalera/code/rest-ws/project/aiorest_ws/command_line.py", line 37, in define
    type=type)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1349, in add_argument
    return self._add_action(action)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1709, in _add_action
    self._optionals._add_action(action)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1553, in _add_action
    action = super(_ArgumentGroup, self)._add_action(action)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1363, in _add_action
    self._check_conflict(action)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1502, in _check_conflict
    conflict_handler(action, confl_optionals)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/argparse.py", line 1511, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument -ip: conflicting option string: -ip

Maybe I something don't understand, but why it fails? Scope="function" means, that this fixture called every time, for every new function, right? First test don't failing, but all listed below – does. Why is this happening?

Aucun commentaire:

Enregistrer un commentaire