Running python Unittest in Jenkins - python

I am a new programmer. I have a simple calculator python code which i want to deploy using Jenkins pipeline.
There is a python unittest file in my git repo named "unit-test". I tried to execute unit test as mentioned in the stage(test), but it is not working.
stage('Test') {
steps {
echo 'Testing..'
python setup.py nosetests --with-xunit
nosetests unit-test.py
Can someone please help me fix the issue.
My github repo url: https://github.com/Tasfiq23/Devops_assessment_2.git/

I have re-created your pipeline in Jenkins and it got success.
Pipeline Script
pipeline {
agent any
stages {
stage ('GIT Checkout'){
steps {
git changelog: false, poll: false, url: 'https://github.com/Tasfiq23/Devops_assessment_2.git'
}
}
stage('build') {
steps {
sh 'pip install -r requirements.txt'
}
}
stage ('Test'){
steps {
sh 'python unit-test.py'
}
}
}
}
Output:

Related

coverage report post step not picking up data in previous step

I have a post always step in Jenkinsfile that runs coverage report -m. The reason why it's in the post step is I want coverage report, regardless even if tests fail. I see that it's running but it's not picking up data from the test phase: python3 -m coverage run -m pytest -v tests
In Jenkinsfile, I have:
stage('Test') {
steps {
dir('src') {
sh "python3 -m coverage run -m pytest -v tests"
}
}
post {
always {
sh "python3 -m coverage report -m"
}
}
}
coverage report -m reports that there's no data: No data to report
I needed to run coverage report -m in the same directory as the one I ran for tests.
stage('Test') {
steps {
dir('src') {
sh "python3 -m coverage run -m pytest -v tests"
}
}
post {
always {
dir('src') {
sh "python3 -m coverage report -m"
}
}
}
}

How to get docker work / run python in Jenkins pipeline?

Here is the story...
i trying build a jenkins pipeline to automate a python test.
when i'm trying run 'pip install xxxx'. The jenkins built process returned pip command not found error. i notice jenkins might require python in the runtime and jenkins by default doesn't come with python.
Hence i trying to pull a docker image in pipeline.
added line "agent{ docker { image 'python:3.7.2' } }"
however docker not found error occurred during the build process
i have installed docker plugin on jenkins, but the docker error still remain...
the question is how i can make the docker work or
how i can run python in jenkins pipeline? as all i'm doing this cos i want to run python tests in jenkins pipeline.
here is the example code
pipeline {
agent{ docker { image 'python:3.7.2' } }
}
stages {
stage('checkout') {
steps{
xxxxxx
}
}
stage('setup env') {
steps {
sh 'pip install -r dependencies.txt'
}
}
stage('run test') {
steps {
sh 'pytest'
}
}
}

Run a Python script in Jenkins Pipeline using Windows

I didn't found a solution for Windows:
This is only working for Linux:
How can I call a py script in a Jenkins File (Pipeline)?
pipeline {
agent { docker { image 'python:3.8.1' } }
stages {
stage('build') {
steps {
sh 'python --version'
}
}
}
}
As you know, the sh command let you invoke the Linux shell.
To execute your Python script on Windows, you should have the following step:
steps {
bat 'python --version'
}

execute pytest using pipeline in Jenkins

currently i execute my tests in pytest by command like "pytest mytest.py" copied to inside form field "Execute Windows batch command" in the job Jenkins. I want to change my job to execute it by pipeline. I try a lot of code from Stackoverflow but any one of them doesn't work. Do you have a simple code to run regresion tests with pytest connection to Git?
I'll suggest you to use : 'Pyenv Pipeline' plugin (https://plugins.jenkins.io/pyenv-pipeline)
stage("test PythonEnv") {
withPythonEnv('python3') {
sh 'pip install pytest'
sh 'pytest mytest.py'
}
}
If you are just after running it as simple Jenkins pipeline (say scripted for now), you can run something like below?
node
{
stage('Run pytest')
{
bat "pytest mytest.py"
}
}

How do I properly set up my Python dependencies within Jenkins using pip and virtualenv?

I am a rookie to Jenkins trying to set up a Python pytest test suite to run. In order to properly execute the test suite, I have to install several Python packages. I'm having trouble with this particular step because Jenkins consistently is unable to find virtualenv and pip:
pipeline {
parameters {
gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'BRANCH', type: 'PT_BRANCH', quickFilterEnabled: true
}
agent any
stages {
stage('Checkout source code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '------', url: 'git#github.com:path-to-my-repo/my-test-repo.git']]])
}
}
stage('Start Test Suite') {
steps {
sh script: 'PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin/:$PATH'
echo "Checking out Test suite repo."
sh script: 'virtualenv venv --distribute'
sh label: 'install deps', script: '/Library/Frameworks/Python.framework/Versions/3.6/bin/pip install -r requirements.txt'
sh label: 'execute test suite, exit upon first failure', script: 'pytest --verbose -x --junit-xml reports/results.xml'
post {
always {
junit allowEmptyResults: true, testResults: 'reports/results.xml'
}
}
}
}
On the virtualenv venv --distribute step, Jenkins throws an error (I'm running this initially on a Jenkins server on my local instance, although in production it will be on an Amazon Linux 2 machine):
virtualenv venv --distribute /Users/Shared/Jenkins/Home/workspace/my-project-name#tmp/durable-5045c283/script.sh:
line 1: virtualenv: command not found
Why is this happening? The step before, I make sure to append where I know my virtualenv and pip are:
sh script: 'PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin/:$PATH'
For instance, when I type in
sudo su jenkins
which pip
which virtualenv
I get the following outputs as expected:
/Library/Frameworks/Python.framework/Versions/3.6/bin/pip
/Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenv
Here are the things I do know:
Jenkins runs as a user called jenkins
best practice is to create a virtual environment, activate it, and the perform my pip installations inside there
Jenkins runs sh by default, not bash (but I'm not sure if this has anything to do with my problem)
Why is Jenkins unable to find my virtualenv? What's the best practice for installing Python libraries for a Jenkins build?
Edit: I played around some more and found a working solution:
I don't know if this is the proper way to do it, but I used the following syntax:
withEnv(['PATH+EXTRA=/Library/Frameworks/Python.framework/Versions/3.6/bin/']) {
sh script: "pip install virtualenv"
// do other setup stuff
}
However, I'm now stuck w/ a new issue: I've clearly hardcoded in my Python path here. If I'm running on a remote Linux machine, am I going to have to install that specific version of Python (3.6)?

Categories