diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b0b0c3ee9..a34357b91 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -56,9 +56,8 @@ jobs: - name: "Run tests" run: | - cd src - coverage run --rcfile ../pyproject.toml manage.py test . --noinput --parallel=4 - coverage xml --rcfile ../pyproject.toml + coverage run --rcfile ./pyproject.toml -m pytest -n auto + coverage xml --rcfile ./pyproject.toml env: POSTGRES_HOST: "localhost" POSTGRES_PORT: 5432 diff --git a/pyproject.toml b/pyproject.toml index 1a63bd5fc..50d03d839 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,9 @@ dev = [ test = [ "bornhack-website[bootstrap]", + "pytest==9.0.2", + "pytest-xdist==3.8.0", + "pytest-django==4.11.1", "beautifulsoup4==4.14.3", "coverage==7.13.0", "hypothesis==6.150.3", @@ -113,7 +116,6 @@ ignore = [ "ARG002", # Unused method argument "EM101", # Exception must not use a string literal, assign to variable first "EM102", # Exception must not use a f-string literal, assign to variable first - "PT", # We use django tests, not pytest ] [tool.ruff.lint.per-file-ignores] @@ -181,3 +183,5 @@ exclude_also = [ ############ PYTEST ############ [tool.pytest.ini_options] DJANGO_SETTINGS_MODULE = "bornhack.settings" +python_files = "tests.py test_*.py" +pythonpath = ". src" diff --git a/src/bornhack/settings.py b/src/bornhack/settings.py index 35007dec3..d557f09d5 100644 --- a/src/bornhack/settings.py +++ b/src/bornhack/settings.py @@ -280,7 +280,7 @@ "tasker": "Team Tasker - task management", } -FIXTURE_DIRS = ["testdata"] +FIXTURE_DIR = BASE_DIR / "testdata" CACHES = { "default": { @@ -288,3 +288,6 @@ "LOCATION": "tile-cache", }, } + +# Use pytest as test runner for integrating with `./manage.py test` +TEST_RUNNER = "pytest_django.runner.TestRunner" diff --git a/src/economy/tests.py b/src/economy/tests.py index 9af5cd210..04fd622f2 100644 --- a/src/economy/tests.py +++ b/src/economy/tests.py @@ -5,6 +5,7 @@ from django.core.exceptions import ValidationError from django.test import TestCase from django.utils import timezone +from django.conf import settings from .models import Bank from .models import BankAccount @@ -30,13 +31,13 @@ def test_bank_account_csv_import(self): ) # make sure we create 6 transactions - with open("testdata/bank.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "bank.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=";", quotechar='"') created = account.import_csv(reader) self.assertEqual(created, 6) # make sure we create 0 if we load the same file again - with open("testdata/bank.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "bank.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=";", quotechar='"') created = account.import_csv(reader) self.assertEqual(created, 0) @@ -49,7 +50,7 @@ def test_bank_account_csv_import(self): ValidationError, msg="Transaction on 2021-09-01 is before the bank accounts start_date. Transaction text is 'c051c94d-0762-422b-a453-e14402' and amount is -250.00", ): - with open("testdata/bank.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "bank.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=";", quotechar='"') created = account.import_csv(reader) @@ -58,7 +59,7 @@ class CoinifyCSVImportTest(TestCase): def test_coinify_invoice_csv_import(self): # make sure we create 4 invoices with open( - "testdata/coinify-invoices-20200101-20200630.csv", + settings.FIXTURE_DIR / "coinify-invoices-20200101-20200630.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -67,7 +68,7 @@ def test_coinify_invoice_csv_import(self): # make sure we create 0 invoices if the same csv is imported again with open( - "testdata/coinify-invoices-20200101-20200630.csv", + settings.FIXTURE_DIR / "coinify-invoices-20200101-20200630.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -77,7 +78,7 @@ def test_coinify_invoice_csv_import(self): def test_coinify_payout_csv_import(self): # make sure we create 2 payouts with open( - "testdata/coinify-payouts-20210701-20210904.csv", + settings.FIXTURE_DIR / "coinify-payouts-20210701-20210904.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -86,7 +87,7 @@ def test_coinify_payout_csv_import(self): # make sure we create 0 payouts if the same csv is imported again with open( - "testdata/coinify-payouts-20210701-20210904.csv", + settings.FIXTURE_DIR / "coinify-payouts-20210701-20210904.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -96,7 +97,7 @@ def test_coinify_payout_csv_import(self): def test_coinify_balance_csv_import(self): # make sure we create 66 balances with open( - "testdata/coinify-account-balances-20210701-20210904.csv", + settings.FIXTURE_DIR / "coinify-account-balances-20210701-20210904.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -105,7 +106,7 @@ def test_coinify_balance_csv_import(self): # make sure we create 0 balances if the same csv is imported again with open( - "testdata/coinify-account-balances-20210701-20210904.csv", + settings.FIXTURE_DIR / "coinify-account-balances-20210701-20210904.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=",", quotechar='"') @@ -116,13 +117,13 @@ def test_coinify_balance_csv_import(self): class EpayCSVImportTest(TestCase): def test_epay_csv_import(self): # make sure we create 4 epay transactions - with open("testdata/epay_test.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "epay_test.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=";", quotechar='"') created = import_epay_csv(reader) self.assertEqual(created, 3) # make sure we create 0 if the same csv is imported again - with open("testdata/epay_test.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "epay_test.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=";", quotechar='"') created = import_epay_csv(reader) self.assertEqual(created, 0) @@ -131,13 +132,13 @@ def test_epay_csv_import(self): class ClearhausCSVImportTest(TestCase): def test_clearhaus_csv_import(self): # make sure we create 10 clearhaus settlements - with open("testdata/clearhaus_settlements.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "clearhaus_settlements.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=",", quotechar='"') created = import_clearhaus_csv(reader) self.assertEqual(created, 9) # make sure we create 0 if the same csv is imported again - with open("testdata/clearhaus_settlements.csv", encoding="utf-8-sig") as f: + with open(settings.FIXTURE_DIR / "clearhaus_settlements.csv", encoding="utf-8-sig") as f: reader = csv.reader(f, delimiter=",", quotechar='"') created = import_clearhaus_csv(reader) self.assertEqual(created, 0) @@ -145,7 +146,7 @@ def test_clearhaus_csv_import(self): class ZettleImportTest(TestCase): def test_zettle_receipts_import(self): - with open("testdata/Zettle-Receipts-Report-20210101-20210910.xlsx", "rb") as f: + with open(settings.FIXTURE_DIR / "Zettle-Receipts-Report-20210101-20210910.xlsx", "rb") as f: df = ZettleExcelImporter.load_zettle_receipts_excel(f) created = ZettleExcelImporter.import_zettle_receipts_df(df) self.assertEqual(created, 6) @@ -154,7 +155,7 @@ def test_zettle_receipts_import(self): self.assertEqual(created, 0) def test_zettle_balances_import(self): - with open("testdata/Zettle-Account-Statement-Report-20230901-20250903.xlsx", "rb") as f: + with open(settings.FIXTURE_DIR / "Zettle-Account-Statement-Report-20230901-20250903.xlsx", "rb") as f: df = ZettleExcelImporter.load_zettle_balances_excel(f) created = ZettleExcelImporter.import_zettle_balances_df(df) self.assertEqual(created, 4059) @@ -166,7 +167,7 @@ def test_zettle_balances_import(self): class MobilePayImportTest(TestCase): def test_mobilepay_import(self): with open( - "testdata/MobilePay_Transfer_overview_csv_MyShop_25-08-2021_14-09-2021.csv", + settings.FIXTURE_DIR / "MobilePay_Transfer_overview_csv_MyShop_25-08-2021_14-09-2021.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=";", quotechar='"') @@ -175,7 +176,7 @@ def test_mobilepay_import(self): # make sure we create 0 if the same csv is imported again with open( - "testdata/MobilePay_Transfer_overview_csv_MyShop_25-08-2021_14-09-2021.csv", + settings.FIXTURE_DIR / "MobilePay_Transfer_overview_csv_MyShop_25-08-2021_14-09-2021.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=";", quotechar='"') @@ -184,7 +185,7 @@ def test_mobilepay_import(self): # now test importing sales CSV, 3 out of 4 lines are already in from above import with open( - "testdata/MobilePay_Sales_overview_csv_MyShop_25-08-2021_14-09-2021.csv", + settings.FIXTURE_DIR / "MobilePay_Sales_overview_csv_MyShop_25-08-2021_14-09-2021.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=";", quotechar='"') @@ -197,7 +198,7 @@ def test_mobilepay_import(self): ) # make sure we create 0 if the same csv is imported again with open( - "testdata/MobilePay_Sales_overview_csv_MyShop_25-08-2021_14-09-2021.csv", + settings.FIXTURE_DIR / "MobilePay_Sales_overview_csv_MyShop_25-08-2021_14-09-2021.csv", encoding="utf-8-sig", ) as f: reader = csv.reader(f, delimiter=";", quotechar='"')