1

I am using pytest needle for testing purpose.In conftest.py I have created a fixture which have scope="class". In this fixture I am creating a browser instance using needle.driver and using request object I am trying to use it in other test classes.

This is for Python 3.7.4 , needle version 0.3.11 , pytest version 4.6.5.

#content of conftest.py
import  pytest

@pytest.fixture(scope="class")
def setup(needle , request):
    needle.driver.get('https://www.google.com')
    request.cls.driver=needle.driver

---------------------------------------------------------------

#content of test_case1.py
from selenium.webdriver.common.by import By
from selenium import webdriver
import os
from selenium.webdriver.common.keys import Keys
import pytest
import unittest

@pytest.mark.usefixtures("setup")
class test(unittest.TestCase):
   def test_example_element(self):
      self.driver.find_element_by_name("q").send_keys("Selenium")
      self.driver.assert_screenshot('D:\\Screenshot\\XPYgdg', (By.NAME, 'q'))

When I run test_case1.py with command py.test --driver Chrome test_case1.py , I get following error :

ScopeMismatch: You tried to access the 'function' scoped fixture 'needle' with a 'class' scoped request object, involved factories
conftest.py:3:  def setup(needle, request)
Rohit Gawas
  • 267
  • 3
  • 8
  • The `needle` fixture from the `pytest-needle` library has function scope ([source](https://github.com/jlane9/pytest-needle/blob/e17e8d3307a6b543847479864352edc36da60b3d/pytest_needle/plugin.py#L113-L131)), so you can't use that in class-scoped fixtures. You can implement your own class-scoped version of `needle` fixture, though. – hoefling Aug 22 '19 at 14:32
  • 1
    You can also inject the `needle` fixture to your test class via an autouse fixture. Check out [this answer of mine](https://stackoverflow.com/a/50135020/2650249) for an example on how to do that. – hoefling Aug 22 '19 at 14:33
  • Thank you @hoefling for the sugestions. After some research I have found one solution.Refer link (https://the-creative-tester.github.io/Python-Visual-Regression-Testing/ ) for more information. – Rohit Gawas Aug 23 '19 at 01:10

0 Answers0