diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml deleted file mode 100644 index 5206255..0000000 --- a/.github/workflows/pre-commit.yml +++ /dev/null @@ -1,15 +0,0 @@ -on: - pull_request: - branches: [main] - push: - branches: [main] - -jobs: - pre-commit: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 - with: - python-version: '3.10' - - uses: pre-commit/action@v3.0.0 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..60bd6f5 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,35 @@ +on: + pull_request: + branches: [main] + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: '3.10' + - uses: pre-commit/action@v3.0.0 + + run-tests: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + + - name: Install Dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements-dev.txt + + - name: Run Tests + run: | + python -m pytest diff --git a/Makefile b/Makefile index 4b66b19..c5582d7 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ all: @echo "#### functions implemented" @echo "make up ...................... runs main.py" @echo "make pc ...................... runs pre-commit on all files" + @echo "make test .................... runs all tests (pytest)" pc: @echo "[PRE-COMMIT] All files" @@ -9,3 +10,6 @@ pc: up: python3 -m src.main + +test: + python3 -m pytest diff --git a/docs/README-en.md b/docs/README-en.md index ceb7ec0..9bf162f 100644 --- a/docs/README-en.md +++ b/docs/README-en.md @@ -1,5 +1,6 @@ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) ![makefile](https://img.shields.io/badge/makefile-enabled-brightgreen?logo=gmail&logoColor=blue) +[![pytest](https://img.shields.io/badge/pytest-enabled-brightgreen?logo=pytest&logoColor=#0A9EDC)](https://docs.pytest.org/en/7.4.x/) # Discord Bot Não fala inglês? [Clique aqui](https://github.com/Robso-creator/discord_bot/blob/main/README.md) para ver esta página em português diff --git a/docs/README.md b/docs/README.md index b6e97be..e23ec13 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,6 @@ [![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) ![makefile](https://img.shields.io/badge/makefile-enabled-brightgreen?logo=gmail&logoColor=blue) +[![pytest](https://img.shields.io/badge/pytest-enabled-brightgreen?logo=pytest&logoColor=#0A9EDC)](https://docs.pytest.org/en/7.4.x/) # Bot do Discord Don't speak portuguese? [Click here](https://github.com/Robso-creator/discord_bot/blob/main/README-en.md) to view this page in English diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..30b51e2 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,4 @@ +-r requirements.txt + +pytest==7.4.4 +pytest-sugar==0.9.7 diff --git a/src/main.py b/src/main.py index 0bf0df4..e0c6568 100644 --- a/src/main.py +++ b/src/main.py @@ -2,8 +2,6 @@ import discord -import settings - intents = discord.Intents.default() intents.typing = False intents.presences = False @@ -23,5 +21,7 @@ async def on_message(self, message): ) -client = MyClient(intents=intents) -client.run(settings.DISCORD_TOKEN) +if __name__ == '__main__': + import settings + client = MyClient(intents=intents) + client.run(settings.DISCORD_TOKEN) diff --git a/tests/test_bot_funcs.py b/tests/test_bot_funcs.py new file mode 100644 index 0000000..b368138 --- /dev/null +++ b/tests/test_bot_funcs.py @@ -0,0 +1,30 @@ +from unittest.mock import MagicMock + +import pytest + +from src.main import MyClient + + +@pytest.fixture +def mocked_discord_client(monkeypatch): + mocked_client = MagicMock(spec=MyClient) + + monkeypatch.setattr(mocked_client, 'run', MagicMock()) + + return mocked_client + + +def test_on_ready_called(mocked_discord_client): + client = mocked_discord_client() + + client.on_ready() + + assert client.on_ready.called + + +def test_on_message_called(mocked_discord_client): + client = mocked_discord_client() + + client.on_message('Mocked') + + assert client.on_message.called