I finally figured it out working on another problem. The problem was that my test couldn't find an import.
It looks like you get the above error if your test fails to import. This makes sense because the test suite can't import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.
To validate your test case just try import the test case file in python console.
Example:
from project.apps.app1.tests import *
Answer from Chris on Stack OverflowI finally figured it out working on another problem. The problem was that my test couldn't find an import.
It looks like you get the above error if your test fails to import. This makes sense because the test suite can't import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.
To validate your test case just try import the test case file in python console.
Example:
from project.apps.app1.tests import *
Use:
./manage.py shell
followed by
import myapp.tests
to find the nature of the import error.
Getting AttributeError: module 'pytest' has no attribute 'main' error while executing the script | Selenium Python Forum
Pytest - "module 'pytest' has no attribute 'fixture' "
Python Testing with pytest - Chapter 5 example c AttributeError: 'module' object has no attribute 'config'
azure - AttributeError: module 'pytest' has no attribute 'config' - Stack Overflow
I'm trying to use "@pytest.fixture(scope="class")" But am getting this error:
AttributeError: module 'pytest' has no attribute 'fixture'
Am i using the wrong Pytest version? My version is 7.2.1
import pytestfrom selenium import webdriverimport os
@pytest.fixture(scope="class")def init_driver(request):supported_browsers= ['chrome' , 'firefox']
browser = os.environ.get('BROWSER')
driver = webdriver.Chrome()if not browser:raise Exception("The environment variable 'BROWSER' must be set")browser = browser.lower()if browser not in supported_browsers:raise Exception(f"Provided browser '{browser}' is not supported"f"Supported browsers are: {supported_browsers}")if browser in ('chrome'):driver = webdriverelif browser in ('firefox'):driver = webdriver.Firefox()
request.cls.driver = driveryielddriver.quit()
pytest.configglobal was deprecated in pytest==4.0 and removed in pytest==5.0. If you want your tests to be compatible with 5.0, you need to pass the config instance via an autouse fixture in order to access it:
class FunctionalTests(unittest.TestCase):
@pytest.fixture(autouse=True)
def inject_config(self, request):
self._config = request.config
def test_selenium(self):
webAppUrl = self._config.getoption('webAppUrl')
...
This is the same approach that I described in my answer to Pytest fixture for a class through self not as method argument.
for me, I just needed to updated pytest-django to the latest, 4.5.2 (as of 05/16/22).
This issue is caused by an out of date version of pytest. I was able to resolve it by using pip uninstall pytest and pip install pytest one after the other to update to the lastest version of pytest.
In my case, the update proposed by Jacob didn't really help because other requirements were violated. I didn't need pytest in my program, so I just deleted the pytest hook from the PyInstaller. The path for me was
C:\Python37-32\Lib\site-packages\PyInstaller\hooks\hook-pytest.py
So not really a "solution", but still works for me.
The problem was that apperantly there was a py module that overshadowed the interal usage of pytest which caused the error.
I solved it by recreating my environment with the minimal requirments to activate pytest.
You're on pytest 7.x, please use tmp_path fixture instead, which returns a stdlib pathlib.Path instance. The tmpdir fixture is legacy.
The basic usage shown in your question is the same, so you can just find/replace tmpdir with tmp_path in this code.
Maybe your interpreter configured in PyDev is not the same one you're using in the command line?
-- pytest should definitely have pytest.main.
Please run the code below (as a regular launch without unit-testing both from the command line as well as from within Eclipse) and provide the output you have in those cases (it should make clear what's the executable and where pytest is being gotten from).
import sys
def inc(x):
return x + 1
def test_inc_pass():
assert inc(5) == 6
def test_inc_fail():
assert inc(5) == 5
if __name__ == '__main__':
import pytest
print('Running in: %s' % (sys.executable,))
print('PyTest found in: %s' % (pytest.__file__,))
pytest.main(sys.argv)
Strange thing. For the first time, when i run your code from eclipse i got the same error as previously, but after restarting eclipse, i run it one more time and Your code works fine:
Running in: C:\MojeApps\Python365\python.exe
PyTest found in: C:\MojeApps\Python365\lib\site-packages\pytest\__init__.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
rootdir: C:\Development\eclipse_workspace\PySandbox\pytest_so_help
collected 2 items
main.py .F [100%]
================================== FAILURES ===================================
________________________________ test_inc_fail ________________________________
def test_inc_fail():
> assert inc(5) == 5
E assert 6 == 5
E + where 6 = inc(5)
main.py:10: AssertionError
=========================== short test summary info ===========================
FAILED main.py::test_inc_fail - assert 6 == 5
========================= 1 failed, 1 passed in 0.03s =========================
and running "my tests" via eclipse gives me new error :)
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-6.0.0, py-1.9.0, pluggy-0.13.1
rootdir: C:\Development\eclipse_workspace\MyPyTests\src
collected 2 items
tests\test_sample.py .F
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 240, in wrap_session
INTERNALERROR> session.exitstatus = doit(config, session) or 0
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 296, in _main
INTERNALERROR> config.hook.pytest_runtestloop(session=session)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\main.py", line 321, in pytest_runtestloop
INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 100, in pytest_runtest_protocol
INTERNALERROR> runtestprotocol(item, nextitem=nextitem)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 117, in runtestprotocol
INTERNALERROR> reports.append(call_and_report(item, "call", log))
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\_pytest\runner.py", line 211, in call_and_report
INTERNALERROR> hook.pytest_runtest_logreport(report=report)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\hooks.py", line 286, in __call__
INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\manager.py", line 87, in <lambda>
INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 208, in _multicall
INTERNALERROR> return outcome.get_result()
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 80, in get_result
INTERNALERROR> raise ex[1].with_traceback(ex[2])
INTERNALERROR> File "C:\MojeApps\Python365\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR> res = hook_impl.function(*args)
INTERNALERROR> File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydev_runfiles\pydev_runfiles_pytest2.py", line 265, in pytest_runtest_logreport
INTERNALERROR> exc = _get_error_contents_from_report(report)
INTERNALERROR> File "C:\MojeApps\eclipse\plugins\org.python.pydev.core_7.6.0.202006041357\pysrc\_pydev_runfiles\pydev_runfiles_pytest2.py", line 167, in _get_error_contents_from_report
INTERNALERROR> tw = TerminalWriter(stringio=True)
INTERNALERROR> TypeError: __init__() got an unexpected keyword argument 'stringio'
========================= 1 failed, 1 passed in 0.05s =========================
I'm starting to think that pydev plugin is broken...
EDIT: Weird thing. After the night when I booted OS, and check if it still appears - it suddenly started to work without a problem - I have no idea...