Merge pull request #2946 from merwanehamadi/feature/add-decorator-to-tests

add decorator to tests so it's skipped if api key required but not present in the environment
pull/2961/head
Nicholas Tindle 2023-04-22 16:50:10 -05:00 committed by Reinier van der Leer
commit 986bdaab36
No known key found for this signature in database
GPG Key ID: 64035FE419545762
7 changed files with 35 additions and 41 deletions

View File

@ -71,8 +71,9 @@ jobs:
- name: Run unittest tests with coverage
run: |
pytest --cov=autogpt --without-integration --without-slow-integration --cov-report term-missing --cov-branch --cov-report xml --cov-report term
pytest --cov=autogpt --cov-report term-missing --cov-branch --cov-report xml --cov-report term
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
- name: Generate coverage report
run: |
coverage report

View File

@ -1,37 +0,0 @@
name: Goal Oriented Tasks
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.10']
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Generate coverage report
run: |
coverage run --source=autogpt -m pytest -s -k tests/integration/goal_oriented
env:
OPENAI_API_KEY: 'dummy_api_key'

3
tests/conftest.py Normal file
View File

@ -0,0 +1,3 @@
from dotenv import load_dotenv
load_dotenv()

View File

@ -2,7 +2,6 @@ import concurrent
import os
import unittest
import pytest
import vcr
from autogpt.agent import Agent
@ -14,6 +13,7 @@ from autogpt.memory import get_memory
# from autogpt.prompt import Prompt
from autogpt.workspace import WORKSPACE_PATH
from tests.integration.goal_oriented.vcr_helper import before_record_request
from tests.utils import requires_api_key
current_file_dir = os.path.dirname(os.path.abspath(__file__))
# tests_directory = os.path.join(current_file_dir, 'tests')
@ -27,7 +27,7 @@ my_vcr = vcr.VCR(
CFG = Config()
@pytest.mark.integration_test
@requires_api_key("OPENAI_API_KEY")
def test_write_file() -> None:
# if file exist
file_name = "hello_world.txt"

View File

@ -7,6 +7,7 @@ import unittest
import pytest
from autogpt.memory.local import LocalCache
from tests.utils import requires_api_key
def mock_config() -> dict:
@ -32,17 +33,20 @@ class TestLocalCache(unittest.TestCase):
self.cfg = mock_config()
self.cache = LocalCache(self.cfg)
@requires_api_key("OPENAI_API_KEY")
def test_add(self) -> None:
"""Test adding a text to the cache"""
text = "Sample text"
self.cache.add(text)
self.assertIn(text, self.cache.data.texts)
@requires_api_key("OPENAI_API_KEY")
def test_clear(self) -> None:
"""Test clearing the cache"""
self.cache.clear()
self.assertEqual(self.cache.data.texts, [])
@requires_api_key("OPENAI_API_KEY")
def test_get(self) -> None:
"""Test getting a text from the cache"""
text = "Sample text"
@ -50,6 +54,7 @@ class TestLocalCache(unittest.TestCase):
result = self.cache.get(text)
self.assertEqual(result, [text])
@requires_api_key("OPENAI_API_KEY")
def test_get_relevant(self) -> None:
"""Test getting relevant texts from the cache"""
text1 = "Sample text 1"
@ -59,6 +64,7 @@ class TestLocalCache(unittest.TestCase):
result = self.cache.get_relevant(text1, 1)
self.assertEqual(result, [text1])
@requires_api_key("OPENAI_API_KEY")
def test_get_stats(self) -> None:
"""Test getting the cache stats"""
text = "Sample text"

View File

@ -7,6 +7,7 @@ from PIL import Image
from autogpt.commands.image_gen import generate_image, generate_image_with_sd_webui
from autogpt.config import Config
from autogpt.workspace import path_in_workspace
from tests.utils import requires_api_key
def lst(txt):
@ -18,6 +19,7 @@ class TestImageGen(unittest.TestCase):
def setUp(self):
self.config = Config()
@requires_api_key("OPENAI_API_KEY")
def test_dalle(self):
self.config.image_provider = "dalle"
@ -36,6 +38,7 @@ class TestImageGen(unittest.TestCase):
self.assertEqual(img.size, (512, 512))
image_path.unlink()
@requires_api_key("HUGGINGFACE_API_TOKEN")
def test_huggingface(self):
self.config.image_provider = "huggingface"

18
tests/utils.py Normal file
View File

@ -0,0 +1,18 @@
import os
import pytest
def requires_api_key(env_var):
def decorator(func):
def wrapper(*args, **kwargs):
if not os.environ.get(env_var):
pytest.skip(
f"Environment variable '{env_var}' is not set, skipping the test."
)
else:
return func(*args, **kwargs)
return wrapper
return decorator