Skip to content

Strange test name #151

Closed
Closed

Description

@arut-grigoryan

Python 3.6.8
pytest-6.1.2
cases-2.5.0

Test flask application

test_file

@pytest.fixture
def barToFoo():
    app.config["foo"]["bar"] = "foo"
    yield
    app.config["foo"]["bar"] = "bar"

@parametrize_with_cases("inputs,request_data", prefix="case_create_")
@pytest.mark.usefixtures("clearMongo", "mocked_oid", "mocked_uuid")
def test_create(db, flaskClient, inputs, request_data):
    ...

db, flaskClient, clearMongo, mocked_oid, mocked_uuid - fixtures

case file

class CreateCases(object):
    @case(id="simple")
    def case_create_2(self, barToFoo):
        inputs = ["foo", "bar"]
        request_data = {"foo": "dfg", "data": "asd"}
        return inputs, request_data

Test name is test_create[inputs_request_data_is_simple]. Without fixture barToFoo in params case_create_2, test name is test_create[simple].
How do i get the name test_create[simple] with fixture barToFoo?

Activity

  1. smarie commented on Dec 2, 2020

    @smarie
    Owner

    Thanks @arut-grigoryan for this feedback.

    Well this is actually a limitation of the implementation.

    • when all cases do not require any fixture, a @parametrize decorator is created with argvalues set as a list of lazy_values, each referring to a case. Since none of them is a fixture_ref, @parametrize defaults to pytest.parametrize and the ids correspond to the "simple" version.

    • when at least one case requires a fixture, a new fixture is created for that case. The @parametrize decorator created under the hood will in this situation receive in its argvalues at least one fixture_ref (for that case) instead of a lazy_value. In that case the pytest implementation does not support it because this is a "fixture union" (in some parameters you need a - possibly parametrized - fixture, while in some others you don't). So in that case @parametrize creates a fixture union with several "branches" or so-called "alternatives". An additional parameter is added to the parametrization to describe this branching. This parameter has by default an id schema following the pattern "is". This is why you get an id of inputs_request_data_is_simple.

    Now there is a workaround if you wish to forget that there is a new fixture created behind the scenes: symply set idstyle=None or idstyle='nostyle'.

    I added a test for your case just as a reference, and updated the documentation. Let me know if this looks sufficient to you (feel free to reopen the ticket if this is not).

    I admit that this sounds complex, but fixture unions are complex, actually. I try my best to hide this complexity and auto-simplify things when possible but sometimes I was not able to find an elegant simplification, in particular for the ids.

    See also here for a detailed explanation on fixture unions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions