METADATA 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. Metadata-Version: 2.1
  2. Name: pytest-lazy-fixtures
  3. Version: 1.1.2
  4. Summary: Allows you to use fixtures in @pytest.mark.parametrize.
  5. Home-page: https://github.com/dev-petrov/pytest-lazy-fixtures
  6. License: MIT
  7. Keywords: tests,pytest,lazy,fixture
  8. Author: Petrov Anton
  9. Author-email: antonp2@yandex.ru
  10. Requires-Python: >=3.8,<4.0
  11. Classifier: License :: OSI Approved :: MIT License
  12. Classifier: Programming Language :: Python :: 3
  13. Classifier: Programming Language :: Python :: 3.8
  14. Classifier: Programming Language :: Python :: 3.9
  15. Classifier: Programming Language :: Python :: 3.10
  16. Classifier: Programming Language :: Python :: 3.11
  17. Classifier: Programming Language :: Python :: 3.12
  18. Requires-Dist: pytest (>=7)
  19. Project-URL: Repository, https://github.com/dev-petrov/pytest-lazy-fixtures
  20. Description-Content-Type: text/markdown
  21. # pytest-lazy-fixtures
  22. [![codecov](https://codecov.io/gh/dev-petrov/pytest-lazy-fixtures/branch/master/graph/badge.svg)](https://codecov.io/gh/dev-petrov/pytest-lazy-fixtures)
  23. [![CI](https://github.com/dev-petrov/pytest-lazy-fixtures/workflows/CI/badge.svg)](https://github.com/dev-petrov/pytest-lazy-fixtures/actions/workflows/ci-test.yml)
  24. [![PyPI version](https://badge.fury.io/py/pytest-lazy-fixtures.svg)](https://pypi.org/project/pytest-lazy-fixtures/)
  25. [![PyPI downloads](https://img.shields.io/pypi/dm/pytest-lazy-fixtures)](https://pypistats.org/packages/pytest-lazy-fixtures)
  26. Use your fixtures in `@pytest.mark.parametrize`.
  27. This project was inspired by [pytest-lazy-fixture](https://github.com/TvoroG/pytest-lazy-fixture).
  28. Improvements that have been made in this project:
  29. 1. You can use fixtures in any data structures
  30. 2. You can access the attributes of fixtures
  31. 3. You can use functions in fixtures
  32. ## Installation
  33. ```shell
  34. pip install pytest-lazy-fixtures
  35. ```
  36. ## Usage
  37. To use your fixtures inside `@pytest.mark.parametrize` you can use `lf` (`lazy_fixture`).
  38. ```python
  39. import pytest
  40. from pytest_lazy_fixtures import lf
  41. @pytest.fixture()
  42. def one():
  43. return 1
  44. @pytest.mark.parametrize('arg1,arg2', [('val1', lf('one'))])
  45. def test_func(arg1, arg2):
  46. assert arg2 == 1
  47. ```
  48. `lf` can be used with any data structures. For example, in the following example, `lf` is used in the dictionary:
  49. ```python
  50. import pytest
  51. from pytest_lazy_fixtures import lf
  52. @pytest.fixture()
  53. def one():
  54. return 1
  55. @pytest.mark.parametrize('arg1,arg2', [('val1', {"value": lf('one')})])
  56. def test_func(arg1, arg2):
  57. assert arg2 == {"value": 1}
  58. ```
  59. You can also specify getting an attribute through a dot:
  60. ```python
  61. import pytest
  62. from pytest_lazy_fixtures import lf
  63. class Entity:
  64. def __init__(self, value):
  65. self.value = value
  66. @pytest.fixture()
  67. def one():
  68. return Entity(1)
  69. @pytest.mark.parametrize('arg1,arg2', [('val1', lf('one.value'))])
  70. def test_func(arg1, arg2):
  71. assert arg2 == 1
  72. ```
  73. And there is some useful wrapper called `lfc` (`lazy_fixture_callable`).
  74. It can work with any callable and your fixtures, e.g.
  75. ```python
  76. import pytest
  77. from pytest_lazy_fixtures import lf, lfc
  78. class Entity:
  79. def __init__(self, value):
  80. self.value = value
  81. def __str__(self) -> str:
  82. return str(self.value)
  83. def sum(self, value: int) -> int:
  84. return self.value + value
  85. @pytest.fixture()
  86. def entity():
  87. return Entity(1)
  88. @pytest.fixture()
  89. def two():
  90. return 2
  91. @pytest.fixture()
  92. def three():
  93. return 3
  94. @pytest.fixture()
  95. def entity_format():
  96. def _entity_format(entity: Entity):
  97. return {"value": entity.value}
  98. return _entity_format
  99. @pytest.mark.parametrize(
  100. "message",
  101. [
  102. lfc(
  103. "There is two lazy fixture args, {} and {}! And one kwarg {field}! And also simple value {simple}".format,
  104. lf("entity"),
  105. lf("two"),
  106. field=lf("three"),
  107. simple="value",
  108. ),
  109. ],
  110. )
  111. def test_lazy_fixture_callable_with_func(message):
  112. assert message == "There is two lazy fixture args, 1 and 2! And one kwarg 3! And also simple value value"
  113. @pytest.mark.parametrize("formatted", [lfc("entity_format", lf("entity"))])
  114. def test_lazy_fixture_callable_with_lf(formatted, entity):
  115. assert formatted == {"value": entity.value}
  116. @pytest.mark.parametrize("result", [lfc("entity.sum", lf("two"))])
  117. def test_lazy_fixture_callable_with_attr_lf(result):
  118. assert result == 3
  119. ```
  120. ## Contributing
  121. Contributions are very welcome. Tests can be run with `pytest`.
  122. ## License
  123. Distributed under the terms of the `MIT` license, `pytest-lazy-fixtures` is free and open source software
  124. ## Issues
  125. If you encounter any problems, please file an issue along with a detailed description.