TvoroG / pytest-lazy-fixture

It helps to use fixtures in pytest.mark.parametrize
MIT License
377 stars 30 forks source link

AttributeError: module 'pytest' has no attribute 'lazy_fixture' #22

Open CristhianBoujon opened 6 years ago

CristhianBoujon commented 6 years ago

I have installed pytest-lazy-fixture:

pip install pytest-lazy-fixture
Collecting pytest-lazy-fixture
  Downloading pytest-lazy-fixture-0.4.0.tar.gz
Collecting pytest>=2.9.2 (from pytest-lazy-fixture)
  Downloading pytest-3.4.0-py2.py3-none-any.whl (188kB)
    100% |████████████████████████████████| 194kB 645kB/s 
Requirement already satisfied: setuptools in /home/overflow012/.virtualenvs/split_dataset/lib/python3.5/site-packages (from pytest>=2.9.2->pytest-lazy-fixture)
Collecting py>=1.5.0 (from pytest>=2.9.2->pytest-lazy-fixture)
  Downloading py-1.5.2-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 1.0MB/s 
Collecting pluggy<0.7,>=0.5 (from pytest>=2.9.2->pytest-lazy-fixture)
  Downloading pluggy-0.6.0.tar.gz
Requirement already satisfied: six>=1.10.0 in /home/overflow012/.virtualenvs/split_dataset/lib/python3.5/site-packages (from pytest>=2.9.2->pytest-lazy-fixture)
Collecting attrs>=17.2.0 (from pytest>=2.9.2->pytest-lazy-fixture)
  Downloading attrs-17.4.0-py2.py3-none-any.whl
Building wheels for collected packages: pytest-lazy-fixture, pluggy
  Running setup.py bdist_wheel for pytest-lazy-fixture ... done
  Stored in directory: /home/overflow012/.cache/pip/wheels/85/9e/a0/45b3bca5fb2b27171cf4537a2b77a078db0140789c364431e3
  Running setup.py bdist_wheel for pluggy ... done
  Stored in directory: /home/overflow012/.cache/pip/wheels/df/44/8e/e136760ae525eac46b3e3db643ef58ff1753177b5a722b0c96
Successfully built pytest-lazy-fixture pluggy
Installing collected packages: py, pluggy, attrs, pytest, pytest-lazy-fixture
Successfully installed attrs-17.4.0 pluggy-0.6.0 py-1.5.2 pytest-3.4.0 pytest-lazy-fixture-0.4.0

When I run pytest I get error:

____________________________________________________ ERROR collecting test/test_split_dataset.py _____________________________________________________
test/test_split_dataset.py:28: in <module>
    "source": pytest.lazy_fixture("dataset_path")
E   AttributeError: module 'pytest' has no attribute 'lazy_fixture'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================== 1 error in 1.20 seconds ===============================================================

Tests:

@pytest.fixture(scope="module", autouse=True)
def dataset_path(tmpdir_factory):
    p = tmpdir_factory.mktemp("tmp").join("dataset.csv")
    df = pd.DataFrame({
        "a": [1, 2, 3, 4, 5, 6],
        "b": [1, 2, 3, 4, 5, 6],
        "c": [1, 2, 3, 4, 5, 6],
        "d": [1, 2, 3, 4, 5, 6],
    })
    df.to_csv(p, index=False)

    yield str(p)  # all code below it will be executed when session done
    os.remove(str(p))

@pytest.mark.parametrize("test_input,expected", [
    ({
        "input": {
            "format": "csv",
            "source": pytest.lazy_fixture("dataset_path")
        },
        "split": {
            "targets": "c",
            "seeds": [1, 5, 34],
            "params":{
                "train_size": 0.7,
                "random_state": 42
            }
        }
    }, 8)
])
def test_dataset(test_input, expected):
    print(test_input)
    assert False
TvoroG commented 6 years ago

Hi! Thanks for notifying. I think it should be because of this https://github.com/TvoroG/pytest-lazy-fixture/issues/16. Try to import lazy_fixture:

from pytest_lazyfixture import lazy_fixture

and use it instead of pytest.lazy_fixture.

TvoroG commented 6 years ago

Actually, I just know realized that you are using lazy_fixture not quite right. lazy_fixture should be the value of pytest.mark.parametrize argument. In your case it can be rewritten like this:

@pytest.mark.parametrize("test_input,test_input_source,expected", [
    ({
        "input": {
            "format": "csv",
        },
        "split": {
            "targets": "c",
            "seeds": [1, 5, 34],
            "params":{
                "train_size": 0.7,
                "random_state": 42
            }
        }
    }, pytest.lazy_fixture("dataset_path"), 8)
])
def test_dataset(test_input, test_input_source, expected):
    print(test_input)
    assert False
CristhianBoujon commented 6 years ago

test_input represents a json and input.source is a required property of that json which is dynamic and I need to fill it with a path generated with dataset_path fixture

TvoroG commented 6 years ago

Sorry but I'm not fully understand your problem :) You have this json which has required input.source property. input.source has to be generated by dataset_path. Is it possible to restore this field inside of test?

@pytest.mark.parametrize("test_input,test_input_source,expected", [
    ({
        "input": {
            "format": "csv",
        },
        "split": {
            "targets": "c",
            "seeds": [1, 5, 34],
            "params":{
                "train_size": 0.7,
                "random_state": 42
            }
        }
    }, pytest.lazy_fixture("dataset_path"), 8)
])
def test_dataset(test_input, test_input_source, expected):
    test_input['input']['source'] = test_input_source
    print(test_input)
    assert False
CristhianBoujon commented 6 years ago

Yes, it is possible, I actually do that but I think this is an issue, anyway