Flask unit test failing due to FileNotFoundError for private key file

I’m trying to run unit tests on a Flask application that utilizes public and private keys for JWT token generation. However, when running my unit tests, I’m getting a FileNotFoundError indicating that the private key file cannot be found.

Here’s the structure of my project:

project/
│
├── app/
│   ├── __init__.py
│   ├── user.py
│   ├── news.py
│   ├── public_key.pem
│   └── private_key.pem
│
└── tests/
    ├── __init__.py
    └── test_app.py

The private_key.pem file is used to sign the JWT tokens in the Flask application. However, when running my unit tests, I receive the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'

Here’s a snippet of the code where the private key is loaded:

# Load the private key
with open('private_key.pem', 'r') as f:
    private_key = f.read()

my terminal:

PS C:UsersferreProjectsPortal-de-Noticias> & c:/Users/ferre/Projects/Portal-de-Noticias/.venv/Scripts/python.exe c:/Users/ferre/Projects/Portal-de-Noticias/backend/user_registration/test_app.py
Arquivo public_key.pem não encontrado.
Traceback (most recent call last):
  File "c:UsersferreProjectsPortal-de-Noticiasbackenduser_registrationtest_app.py", line 4, in <module>
    from user import user_bp
  File "c:UsersferreProjectsPortal-de-Noticiasbackenduser_registrationuser.py", line 54, in <module>     
    with open('private_key.pem', 'r') as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'
PS C:UsersferreProjectsPortal-de-Noticias> 

my test code:

import unittest
from flask import Flask
from flask_cors import CORS
from user import user_bp
from noticias import noticias_bp

class TestApp(unittest.TestCase):
    def setUp(self):
        # Configurar a aplicação Flask para testes
        self.app = Flask(__name__)
        CORS(self.app)
        self.app.secret_key = '2002'
        self.app.register_blueprint(user_bp)
        self.app.register_blueprint(noticias_bp)

        # Configurar o cliente de teste
        self.client = self.app.test_client()

    def test_user_blueprint_registration(self):
        # Verificar se o blueprint do usuário está registrado na aplicação Flask
        self.assertIn('user', self.app.blueprints)

    def test_noticias_blueprint_registration(self):
        # Verificar se o blueprint de notícias está registrado na aplicação Flask
        self.assertIn('noticias', self.app.blueprints)

    def test_user_blueprint_routes(self):
        # Testar se as rotas do blueprint do usuário estão acessíveis
        response = self.client.get('/user/')
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/user/profile')
        self.assertEqual(response.status_code, 200)

    def test_noticias_blueprint_routes(self):
        # Testar se as rotas do blueprint de notícias estão acessíveis
        response = self.client.get('/noticias/')
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/noticias/latest')
        self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
    unittest.main()

How can I solve this issue and get my unit tests to work correctly? Should I include the private key directly in the test code, or is there a better approach to handling this?

Thanks in advance for any help!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật