Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Add asyncio marker and refactor test fixtures

Magnus Müller061e701922a5
diff --git a/pytest.ini b/pytest.ini
index 31fd5bdc3..8715c46f8 100644
--- a/pytest.ini
+++ b/pytest.ini
@@ -3,6 +3,7 @@ markers =
slow: marks tests as slow (deselect with '-m "not slow"')
integration: marks tests as integration tests
unit: marks tests as unit tests
+ asyncio: mark tests as async tests
testpaths =
tests
diff --git a/tests/conftest.py b/tests/conftest.py
index 7de7d66aa..447dba533 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,11 +1,13 @@
"""
Test configuration for browser-use.
"""
+
import logging
import os
import sys
import pytest
+from langchain_openai import ChatOpenAI
# Ensure the project root is in the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
@@ -19,49 +21,38 @@ load_dotenv()
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
-from langchain_core.language_models.fake import FakeListChatModel
-from langchain_openai import ChatOpenAI
from browser_use.browser.browser import Browser, BrowserConfig
from browser_use.browser.context import BrowserContext
[email protected](scope="session")
[email protected](scope='session')
def llm():
- """
- Fixture to provide a ChatOpenAI instance or a mock for testing.
- Uses a mock if OPENAI_API_KEY is not set.
- """
- api_key = os.getenv("OPENAI_API_KEY")
- logger.debug(f"API Key present: {bool(api_key)}")
- if not api_key:
- # Create a mock LLM that always returns a predefined response
- logger.debug("Using FakeListChatModel for testing")
- return FakeListChatModel(
- responses=[
- "Go to a website",
- "Select an option",
- "Perform an action",
- "Test completed"
- ]
- )
- logger.debug("Using actual ChatOpenAI model")
- return ChatOpenAI(model='gpt-4o', api_key=api_key)
+ """
+ Fixture to provide a ChatOpenAI instance or a mock for testing.
+ Uses a mock if OPENAI_API_KEY is not set.
+ """
+ api_key = os.getenv('OPENAI_API_KEY')
+ logger.debug(f'API Key present: {bool(api_key)}')
+ logger.debug('Using actual ChatOpenAI model')
+ return ChatOpenAI(model='gpt-4o', api_key=api_key)
[email protected](scope="session")
+
[email protected](scope='session')
def browser():
- """
- Fixture to provide a Browser instance for testing.
- """
- logger.debug("Creating Browser instance for testing")
- return Browser(config=BrowserConfig(headless=True, disable_security=True))
+ """
+ Fixture to provide a Browser instance for testing.
+ """
+ logger.debug('Creating Browser instance for testing')
+ return Browser(config=BrowserConfig(headless=True, disable_security=True))
+
[email protected](scope="function")
[email protected](scope='function')
async def browser_context(browser):
- """
- Fixture to provide a BrowserContext instance for testing.
- """
- logger.debug("Creating BrowserContext instance for testing")
- context = BrowserContext(browser=browser)
- yield context
- await context.close()
+ """
+ Fixture to provide a BrowserContext instance for testing.
+ """
+ logger.debug('Creating BrowserContext instance for testing')
+ context = BrowserContext(browser=browser)
+ yield context
+ await context.close()