METADATA 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. Metadata-Version: 2.1
  2. Name: pytest-lazy-fixtures
  3. Version: 1.1.1
  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://badge.fury.io/py/pytest-lazy-fixtures)
  25. Use your fixtures in `@pytest.mark.parametrize`.
  26. This project was inspired by [pytest-lazy-fixture](https://github.com/TvoroG/pytest-lazy-fixture).
  27. Improvements that have been made in this project:
  28. 1. You can use fixtures in any data structures
  29. 2. You can access the attributes of fixtures
  30. 3. You can use functions in fixtures
  31. ## Installation
  32. ```shell
  33. pip install pytest-lazy-fixtures
  34. ```
  35. ## Usage
  36. To use your fixtures inside `@pytest.mark.parametrize` you can use `lf` (`lazy_fixture`).
  37. ```python
  38. import pytest
  39. from pytest_lazy_fixtures import lf
  40. @pytest.fixture()
  41. def one():
  42. return 1
  43. @pytest.mark.parametrize('arg1,arg2', [('val1', lf('one'))])
  44. def test_func(arg1, arg2):
  45. assert arg2 == 1
  46. ```
  47. `lf` can be used with any data structures. For example, in the following example, `lf` is used in the dictionary:
  48. ```python
  49. import pytest
  50. from pytest_lazy_fixtures import lf
  51. @pytest.fixture()
  52. def one():
  53. return 1
  54. @pytest.mark.parametrize('arg1,arg2', [('val1', {"value": lf('one')})])
  55. def test_func(arg1, arg2):
  56. assert arg2 == {"value": 1}
  57. ```
  58. You can also specify getting an attribute through a dot:
  59. ```python
  60. import pytest
  61. from pytest_lazy_fixtures import lf
  62. class Entity:
  63. def __init__(self, value):
  64. self.value = value
  65. @pytest.fixture()
  66. def one():
  67. return Entity(1)
  68. @pytest.mark.parametrize('arg1,arg2', [('val1', lf('one.value'))])
  69. def test_func(arg1, arg2):
  70. assert arg2 == 1
  71. ```
  72. And there is some useful wrapper called `lfc` (`lazy_fixture_callable`).
  73. It can work with any callable and your fixtures, e.g.
  74. ```python
  75. import pytest
  76. from pytest_lazy_fixtures import lf, lfc
  77. class Entity:
  78. def __init__(self, value):
  79. self.value = value
  80. def __str__(self) -> str:
  81. return str(self.value)
  82. def sum(self, value: int) -> int:
  83. return self.value + value
  84. @pytest.fixture()
  85. def entity():
  86. return Entity(1)
  87. @pytest.fixture()
  88. def two():
  89. return 2
  90. @pytest.fixture()
  91. def three():
  92. return 3
  93. @pytest.fixture()
  94. def entity_format():
  95. def _entity_format(entity: Entity):
  96. return {"value": entity.value}
  97. return _entity_format
  98. @pytest.mark.parametrize(
  99. "message",
  100. [
  101. lfc(
  102. "There is two lazy fixture args, {} and {}! And one kwarg {field}! And also simple value {simple}".format,
  103. lf("entity"),
  104. lf("two"),
  105. field=lf("three"),
  106. simple="value",
  107. ),
  108. ],
  109. )
  110. def test_lazy_fixture_callable_with_func(message):
  111. assert message == "There is two lazy fixture args, 1 and 2! And one kwarg 3! And also simple value value"
  112. @pytest.mark.parametrize("formatted", [lfc("entity_format", lf("entity"))])
  113. def test_lazy_fixture_callable_with_lf(formatted, entity):
  114. assert formatted == {"value": entity.value}
  115. @pytest.mark.parametrize("result", [lfc("entity.sum", lf("two"))])
  116. def test_lazy_fixture_callable_with_attr_lf(result):
  117. assert result == 3
  118. ```
  119. ## Contributing
  120. Contributions are very welcome. Tests can be run with `pytest`.
  121. ## License
  122. Distributed under the terms of the `MIT` license, `pytest-lazy-fixtures` is free and open source software
  123. ## Issues
  124. If you encounter any problems, please file an issue along with a detailed description.