Django: reusable app testing - python

By following official Django doc, I've extracted an app from my project and made it reusable and installable using pip (currently I still have to learn how to release it on pypi but that's another story)... so far so good... but now I have no idea how to run the tests I wrote for my app, since after installing it in my project using pip Django stopped to execute those tests (by default in Django 1.7 only project-apps tests are picked up)... so my question is: how can I run tests for my apps now that it has been extracted from the main project sources?
ps: of course I don't want to force the potential users of my app to run the tests I wrote, but I have to run them while working on the app on my machine

Ok, I'm an idiot... the only thing I have to do is passing the app name in the test command:
python manage.py test -v 2 -p "Test*.py" --noinput --settings=settings.test my_app

Related

Testing python Azure function app before deployment using Github Workflows

I have a python function app that I want I am deploying to Azure using a github standard workflow. I want to add a job before deploying that also runs all the units tests for the function. Locally, I am using pytest to run the unit tests. The structure of the folders is as shown below.
When running locally I do 'python -m pytest tests' and it runs all the tests.
When I add the same thing in the github workflow it gives me error
ERROR: file or directory not found: tests
The job is described as:
Is there a way to run the tests using the workflow?
I was able to solve this issue by using the command
python -m pytest ./function/tests

How to deploy a Node.js application with a child process (python) in Heroku?

I'm trying to deploy a Node.js application with a child process that runs a machine learning algorithm. I can use this locally, but when I try to run at the Heroku server I recive some messages calling that are some libraries missing, like bellow:
ModuleNotFoundError: No module named 'pandas'
I tried to create manually the requirements.txt and put the necessary libraries there:
pandas
pymongo
dnspython
scikit-learn
scipy
selenium
webdriver-manager
textblob
But it doesn't work. Do I need to do some extra configuration?
Thank you so much for your help!
The way your Heroku dynos run your software is through something called a buildpack.
When you deploy an application to Heroku, it looks at your code and tries to figure out which programming language you are using, then based on that, will run your app using the corresponding buildpack.
For example, if you deploy an app to Heroku and the app has a package.json file in the root of your project directory, Heroku will assume your app is a JavaScript app and use the Node.js buildpack.
Buildpacks contain a number of pre-installed dependencies. For example, the Node.js buildpack contains node (so you can run your JavaScript code) as well as a number of Linux dependencies so that your app will be able to install common libraries/tools that might rely on them.
But... One downside of this buildpack strategy is that if you're deploying a Node.js app, for example, the default Node.js building will NOT come with Python and the various Python library dependencies installed. This is because Heroku supports a lot of different programming environments, and it would be slow/complex if there was just a single buildpack that had EVERYTHING installed. It'd be crazy!
So what you need to do, in your case, is use multiple buildpacks! Heroku has a way for you to enable multiple buildpacks for your app so that your app can have the Node.js dependencies as well as the Python dependencies, for example!
This article on Heroku's documentation site explains how to use multiple buildpacks for a given app.
Here are the specific instructions for simplicity's sake:
# This command will set your default buildpack to Node.js
$ heroku buildpacks:set heroku/nodejs
# This command will set it up so that the Heroku Python buildpack will run first
$ heroku buildpacks:add --index 1 heroku/python
By doing the above, you'll be able to have Heroku install your Python dependencies via a traditional requirements.txt file like you would with any normal Python application.

How can I use django's default test framework to test a module included in sys.path that is not a subdirectory of the django project?

I have a django (1.8) site that is structured like this:
.../django_project/
./templates/
./manage.py
./<dir with settings,urls,etc>
.../django_app_project/
./app_name/
./<app files>
./test_app.py
To run the site, I set the environment variable PYTHONPATH=.../django_app_project/. This works perfectly when running the server, but causes problems when running the tests. Django's DiscoverRunner (django.test.runner.DiscoverRunner) does not notice .../Django_app_project/app_name/test_app.py. How can this be made to work without moving the app into the Django project? (It is outside for version control reasons)

Launching an app in heroku? What is procfile? 'web:' command?

I was referring to this site as i am learning python/flask and trying to use heroku.
http://ryaneshea.com/lightweight-python-apps-with-flask-twitter-bootstrap-and-heroku
Let me explain what all I did, so that any one who is stuck like me can get the picture.
I am using Linux Backtrack command line .
1. I started virtualenv and then installed flask
virtualenv --distribute
pip install flask
Then, connected heroku and github, created a repo also in the github.
Wrote a simple script and saved it in app.py
Then, asked to create a procfile!
web: python app.py
Questions.
1. What is procfile in layman terms?
2. When i type web: python app.py, it says 'web:: command not found'
Please elaborate how this works?
I have one more doubt, sudo is supreme user right?
We are not supposed to use it in virtualenv?
And for what exactly are we using virtualenv? A simple example.
Questions are pretty basic. DO bare.
the Procfile tells Heroku what commands should be run (https://devcenter.heroku.com/articles/procfile).
You are able to define difference process types, such as web (the only one which will autostart by default), workers, etc...
So basically a Procfile containing
web: python app.py
is telling Heroku to started a named process called web, and to run python app.py when it starts.
There is Python specific documentation for Heroku at https://devcenter.heroku.com/articles/getting-started-with-python#declare-process-types-with-procfile

Heroku Django: Reconnecting and Updating

I've finished the Heroku tutorial on how to upload and launch a basic django based web app using the following:
https://devcenter.heroku.com/articles/django#gitignore
Can anybody provide steps or a link to documentation on how to re-connect to Heroku and properly upload changes you've made to your site? Starting from a fresh terminal and cd'ing into the folder where your manage.py and procfiles live, what would the following steps be?
I then do:
$ virtualenv venv --distribute
$ source venv/bin/activate
after this i try to run $python manage.py runserver but it can't find django modules etc...
Do I need to reinstall django everytime I go to git push an update on the server?
To answer your question:
Heroku is just git, basically. At least on your computer.
So you don't need to "connect" to Heroku. You just cd to your source folder and use it as you would a normal git repository.
If you're using a virtualenv (which you should be), after you cd to your directory, you'll need to reactivate the virtualenv. If you followed heroku's Django tutorial, it'll probably be the command source venv/bin/activate
If you want to dig into some of herokus commands, download the Toolbelt.
To test whether your Procfile is working, use Honcho. It's a python version of foreman, Heroku's Ruby-based local Procfile runner.
To see which files are on your Heroku app:
Launch the bash on heroku by typing this command on your terminal:
heroku run bash --app appname
From there just ls and see your folders.

Categories