gitlab-ci.yml: 'script: -pytest' command is not recognized - python

I'm having trouble implementing an sample program that runs pytest within .gitlab-ci.yml on Windows:
Using Shell executor...
Please find below .gitlab-ci.yml:
# .gitlab-ci.yml
test_sample:
stage: test
tags:
- test_sam
script:
- echo "Testing"
- pytest -s Target\tests
when: manual
CI/CD terminal output:
pytest is not recognized as the name of a cmdlet, function, script file, or operable program.
Python and pytest is already installed on the Windows OS on which the test is running but still the test fails.
I have tried the solution suggested in below thread but it doesn't work:
gitlab-ci.yml: 'script: -pytest cannot find any tests to check'
Could you please suggest how to make this test pass on windows?

If python is recognized, you could replace pytest, as with this similar project, with:
unittests:
script: python -m unittest discover tests -v
core doctests:
script: python -m doctest -v AmpScan/core.py
registration doctests:
script: python -m doctest -v AmpScan/registration.py
align doctests:
script: python -m doctest -v AmpScan/align.py
(The initial switch to pytest failed)
If you want to use pytest, you would need to use a python Docker image in your .gitlab.yml.
See "Setting Up GitLab CI for a Python Application" from Patrick Kennedy.
image: "python:3.7"
before_script:
- python --version
- pip install -r requirements.txt
stages:
- Static Analysis
- Test
...
unit_test:
stage: Test
script:
- pwd
- ls -l
- export PYTHONPATH="$PYTHONPATH:."
- python -c "import sys;print(sys.path)"
- pytest

The below command worked without any issues:
py -m pytest -s Target\tests

Related

py.test is not showing any "print" output even with flag "-s"

On Ubuntu 20.04.5 using pytest 7.2.0 with python 3.8.10 I have a test script which is run by
py.test -s /path/to/script ...
but it does not show any output of print statements. When running it with
py.test -v -s /path/to/script ...
I double checked that the test case I am working on is actually executed - but still no print output.
What could be going on?

'No module named pytest' on Gitlab CI nox session despite installing it and using 'external'

When I launch my code to gitlab it goes through the CI, but the pytest session fails while on local machine doesn't.
On my gitlab-ci.yaml I tried installing pytest inside and outside the requirements.txt file, this is how it looks now:
.test:
tags: ["CsLib"]
before_script:
- python3 -m pip install -r requirements.txt
- python3 -m pip install pytest
pytest:
stage: test
extends: ".test"
script:
- nox -s test
After doing the installation it goes to the pytest defined session, that looks like this:
#session(python=["3.9"])
def test(s: Session) -> None:
s.posargs.append("--no-install")
s.run("python", "-m", "pytest", "tests", external=True)
I have enabled the use of existing virtualenvs, and typed external=True for it to use already installed packages. Yet it gives the error
$ nox -s test
nox > Running session test-3.9
nox > Creating virtual environment (virtualenv) using python3.9 in .nox/test-3-9
nox > python -m pytest tests
/builds/RnD/python-libs/.nox/test-3-9/bin/python: No module named pytest
nox > Command python -m pytest tests failed with exit code 1
nox > Session test-3.9 failed.
The thing is, when doing a session lint that it has almost the same structure as the test session but using flake8 it gives no error.
#session(python=["3.9"])
def lint(s: Session) -> None:
s.posargs.append("--no-install")
s.run("flake8", external=True)
This 'flake8' is installed within the requirements file, I tried doing that with pytest but it does not work.
If I type 'nox -s test' in my local machine it executes it without any problem, so I must be doing something wrong on the CI part that I can not see.

How to run Gitlab matrix builds on both Windows/Powershell and Linux/bash?

We're moving our CI from Jenkins to Gitlab and I'm trying to setup a pipeline that runs on both Windows and Linux.
Running multiple Python versions on a Linux gitlab runner works ok by defining versions like this:
.versions:
parallel:
matrix:
- PYTHON_VERSION: ['3.7', '3.8', '3.9']
OPERATING_SYSTEM: ['linux', 'windows']
then calling them for each stage they are needed
build_wheel:
parallel: !reference [.versions, parallel]
I'm trying to add a Windows runner now and have run into the snag that Powershell syntax is different to bash. Most of the Python calls still work, but calling the activate script needs to be different. How do I switch scripts depending on the operating system?
It doesn't seem to be possible to add rules to a script, so I'm trying something like this
.activate_linux: &activate_linux
rules:
- if: $OPERATING_SYSTEM == 'linux'
script:
- source venv/bin/activate
.activate_windows: &activate_windows
rules:
- if: $OPERATING_SYSTEM == 'windows'
script:
- .\venv\Scripts\activate
.activate: &activate
- *activate_linux
- *activate_windows
before_script:
- python -m venv venv
- *activate
- pip install --upgrade pip wheel "setuptools<60"
but it gives me the error: "before_script config should be a string or a nested array of strings up to 10 levels deep".
Is it possible to have one .gitlab-ci.yml file that works on both Windows and Linux? Surely someone has worked this out, but I can't find any solutions.
You can't easily do this in the yaml with that matrix.
Instead, you can do this:
.scripts:
make_venv:
- python -m venv venv
activate_linux:
- !reference [.scripts, make_venv]
- source ./venv/bin/activate
activate_windows:
- !reference [.scripts, make_venv]
- venv/Scripts/activate.ps1
.job_template:
parallel:
matrix:
- PYTHON_VERSION: ['3.7', '3.8', '3.9']
script:
- pip install --upgrade pip wheel "setuptools<60"
- # ...
build_linux:
extends: .job_template
variables:
OPERATING_SYSTEM: 'linux'
before_script:
- !reference [.scripts, activate_linux]
build_windows:
extends: .job_template
variables:
OPERATING_SYSTEM: 'windows'
before_script:
- !reference [.scripts, activate_windows]

Why pytest-sugar doesn't work in GitLab CI?

When tests are launched in GitLab CI, pytest-sugar doesn't show output like in local launching. What the problem can be?
My gitlab config:
image: project.com/path/dir
stages:
- tests
variables:
TESTS_ENVIRORMENT:
value: "--stage my_stage"
description: "Tests launch my_stage as default"
before_script:
- python3 --version
- pip3 install --upgrade pip
- pip3 install -r requirements.txt
api:
stage: tests
script:
- pytest $TESTS_ENVIRORMENT Tests/API/ -v
Local:
GitLab:
It seems that there's a problem with pytest-sugar inside containers. Add --force-sugar option to pytest call, it worked for me
By default docker container do not allocate a pseudo-terminal(tty) as a result not stdout, its simple output from console.
There is not clear solution for that case, mostly needs to do workarounds and try special python libraries.

How to use python and mongodb on the gitlab (CI/CD)

I have a problem when I was used CI/CD on the gitlab. I always use "python:latest" but it is 2.7.5 version, but I want to use python2.7.15 or python3.7. How can I install it?
-
image: python:latest
services:
- mongo:latest
variables:
MONGO_DB: ipc_alert
cache:
paths:
- ~/.cache/pip/
before_script:
- python -V
- pip install -r req.txt
stages:
- test
test:
stage: test
script:
- echo 'Testing'
On the image you're posting, you have a different problem. You cannot find django on a valid version for your requirements.
About the question itself, if you want to test against multiple versions, you need to create more than one test. For example:
test:
stage: test
script:
- echo 'Testing'
That's will be:
test-python2.7:
stage: test
image: python:2.7
script:
- echo 'Testing'
test-python3.4:
stage: test
image: python:3.4
script:
- echo 'Testing'
test-python3.5:
stage: test
image: python:3.5
script:
- echo 'Testing'
test-python3.6:
stage: test
image: python:3.6
script:
- echo 'Testing'
test-python3.7:
stage: test
image: python:3.7
script:
- echo 'Testing'
test-python.latest:
stage: test
image: python:latest
script:
- echo 'Testing'
However, maybe this doesn't work, because you're using a "Shell executor". If I remember correctly, this runner execute your code against the current machine. You need to install docker and create a new runner who uses these docker. Without it, you cannot test against different environments / versions.
One exception to this, are if you have all python versions you need installed on your machine, and calls each python concrete version. It depends on your environment, but you can check on /usr/bin if you have multiple python versions. On my machine, I have on /usr/bin these ones:
maqui#kanade:~$ python -V
Python 2.7.15+
maqui#kanade:~$ python2.6 -V
Python 2.6.8
maqui#kanade:~$ python2.7 -V
Python 2.7.15+
maqui#kanade:~$ python3.6 -V
Python 3.6.8rc1
maqui#kanade:~$ python3.7 -V
Python 3.7.2rc1
(As you can see, python is an alias for python2.7).

Categories