text: Q:
  
  What is the best way to parameterize a pytest function using a fixture that returns a list?
  
  Right now I have a test file that looks like this contrived example:
  import pytest
  
  def colors():
      # Expensive operation
      return ['red', 'yellow', 'blue']
  
  @pytest.mark.parametrize('color', colors())
  def test_colors(color):
      assert color != 'mauve'
  
  This works fine, but since colors() is an expensive operation, I'd like to take advantage of pytest's caching and make it a session scoped fixture. Furthermore, I'd also like to write other tests using it as a fixture like
  def test_colors_list(colors):
      assert len(colors) == 3
  
  Ideally, my test file would look something like
  @pytest.fixture(scope='session')
  def colors():
      # Expensive operation
      return ['red', 'yellow', 'blue']
  
  @pytest.mark.parametrize('color', colors)
  def test_colors(color):
      assert color != 'mauve'
  
  def test_colors_list(colors):
      assert len(colors) == 3
  
  But this results in an error so I'm not going about this correctly. 
  Ideally, I would also like to reference other fixtures in colors(), as well as still parameterize test_colors() to generate multiple functions. 
  What's the best way I should write these tests? 
  
  A:
  
  One solution is to generate the colors in a separate function and use its result as the params parameter for a fixture, which you can then use multiple times
  import pytest
  
  def colors():
      print('expensive')
      return ['blue', 'red', 'mauve']
  
  @pytest.fixture(params=colors())
  def color(request):
      return request.param
  
  def test_bla(color):
      print(color, end='')
  
  def test_foo(color):
      print(color, end='')
  
  If you run pytest -s you will see the string expensive only once in the output:
  $py.test -sv
  ======================== test session starts =========================
  platform linux -- Python 3.4.3, pytest-3.4.1, py-1.5.2, pluggy-0.6.0 -- /home/nils/test/bin/python3
  cachedir: .pytest_cache
  rootdir: /home/nils/test/bla, inifile:
  collecting 0 items                                                   expensive
  collected 6 items
  
  test_bla.py::test_bla[blue] bluePASSED
  test_bla.py::test_bla[red] redPASSED
  test_bla.py::test_bla[mauve] mauvePASSED
  test_bla.py::test_foo[blue] bluePASSED
  test_bla.py::test_foo[red] redPASSED
  test_bla.py::test_foo[mauve] mauvePASSED
  
  ====================== 6 passed in 0.04 seconds ======================
  
  However, the expensive function is run at import time, which is not a good behaviour.
  
  