You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
0 Hrs
15 Min
27 Seg

Automatizaci贸n de Pruebas Unitarias en Python con GitHub Actions

18/20
Resources

Integrating a test suite into a Continuous Integration (CI) system is key to automating the process of verifying code changes. In this case, we will use GitHub Actions to run our tests automatically every time there is a change in the repository, ensuring that the code is always working correctly.

How to set up your first GitHub Action?

First, access the "Actions" tab inside your GitHub repository. There you will find a Marketplace with several options. Search for "Python" and select the Action "Python Application". This configuration will run tests automatically every time there is a push or pull request to the "Main" branch.

What steps are included in the testing workflow?

  • Repository cloning: The workflow starts by cloning your code, similar to a git clone.
  • Python configuration: Uses Python version 3.10, ensuring compatibility with the project code.
  • Dependency installation: Run the installations of the libraries listed in the requirements.txt file, e.g., Faker and Coverage.
  • Modification of the test command: Instead of using a generic test, the command is changed to python -m unittest discover test, tailored to the project's unit tests.

How to verify if the workflow was successful?

Once the file is configured and the commit is done, you can see the progress of the execution in the "Actions" tab. If everything went well, a green checkmark will appear indicating that the tests passed successfully.

How to improve the test coverage in your pipeline?

The additional challenge is to run the tests with different versions of Python using Matrix in GitHub Actions. This will allow you to test your code in various environments, ensuring greater robustness and avoiding compatibility issues.

Contributions 4

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Pas贸 algo interensante en mi workflow file. Lo primero era que GitHub Actions no coincidia con la zona horaria que tenia en mi local, la de bogota, por eso mismo en el archivo se tuvo que especificar para que al runnear los test, en los withdraw de las horas funcionaran bien. Asi mismo se tuvo que configurar el path para que GitHub Actions reconociera la carpeta especifica de los tests, ya que no la tenia como la tenia el profe. ```txt name: Python Tests on: push: branches: [ main, master ] pull_request: branches: [ main, master ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip pip install requests faker if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run tests env: TZ: 'America/Bogota' run: | cd py-test export PYTHONPATH="${PYTHONPATH}:${PWD}" python -m unittest discover -s test -v ```
Solucion al reto para correr jobs con varias versiones de python. \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- \- Solo cambia la parte de jobs ```js jobs: build: runs-on: ubuntu-latest strategy: matrix: python-version: [ "3.10", "3.9", "3.11" ] steps: - uses: actions/checkout@v4 - 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 flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with unittest run: | python -m unittest discover tests ```jobs:聽 build: 聽 聽 runs-on: ubuntu-latest 聽 聽 strategy:聽 聽 聽 matrix:聽 聽 聽 聽 python-version: \[ "3.10", "3.9", "3.11" ] 聽 聽 steps:聽 聽 - uses: actions/checkout@v4聽 聽 - 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 flake8 pytest聽 聽 聽 聽 if \[ -f requirements.txt ]; then pip install -r requirements.txt; fi聽 聽 - name: Test with unittest聽 聽 聽 run: |聽 聽 聽 聽 python -m unittest discover tests
Luego de volverme un embole por no poder subir bien el archivo (no recordar bien de github) pude hacerlo y as铆 qued贸 el c贸digo de workflow que me generaba error porque cree otra carpeta UnitTesting. ```python name: Python application on: push: branches: [ "master" ] pull_request: branches: [ "master" ] permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python 3.12.7 uses: actions/setup-python@v4 with: python-version: "3.12.7" - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with pytest run: | pytest UnitTesting/tests/ ```
Tuve algunos errores porque usaba decouple para configurar variables de entorno, no sabia el error asi que le puse al comando de python -m unittest discover \[-v] tests verbose para entender que pasaba. Al final solo era configurar los secretos en github actions y listo. ```js name: Python application on: push: branches: [ "main" ] pull_request: branches: [ "main" ] permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python 3.10 uses: actions/setup-python@v3 with: python-version: "3.10" - name: Set environment variables run: echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> $GITHUB_ENV - name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Test with unittest run: | python -m unittest discover -v tests ```name: Python application on: push: branches: \[ "main" ] pull\_request: branches: \[ "main" ] permissions: contents: read jobs: build: runs-on: ubuntu-latest steps: \- uses: actions/checkout@v4 \- name: Set up Python 3.10 uses: actions/setup-python@v3 with: python-version: "3.10" \- name: Set environment variables run: echo "SECRET\_KEY=${{ secrets.SECRET\_KEY }}" >> $GITHUB\_ENV \- name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if \[ -f requirements.txt ]; then pip install -r requirements.txt; fi \- name: Test with unittest run: | python -m unittest discover -v tests