Repository Structure and Python
kennethreitz.com
kennethreitz.com
foo/
+- test/
| +- __init__.py
| +- test_bar.py
| +- test-qux.py
+- __init__.py
+- bar.py
+- qux.py
This means that test-code is automatically part of the package, so you can
easily run your tests in production (assuming they don't depend on services or
modules only installed on your development machines) to check things during
a rollout. Also, because the tests and the code are part of the same top-level
Python project, you don't have to mess around with setting Python's import path
to get the tests running; one "export PYTHONPATH=$PWD" in your terminal will
get code and tests working in one go.Tests that need to be run in production are probably functional or integration tests (Although even those should probably be run on a test system, not the actual production server). These should be separate from unit tests. Will you then have a whole tree of test subdirectories in each of your python submodules? test/unit/py, test/functional/py etc.
> so you can easily run your tests in production
I would be wary about running unit tests on my production box. More specifically unit tests that muck with the database. Sure you might have it setup to point to a test database, but is it really worth it to run the tests there on the off-change that the config gets screwed up and your unittests mess up your production data? > Also, because the tests and the code are part of
> the same top-level Python project, you don't have
> to mess around with setting Python's import path
> to get the tests running
Within the source directory, there is no need to set $PYTHONPATH. You can just run: python setup.py test
You just have to point setup.py at your test packages. Note that this is only for the default unittest tests. I don't have experience with pytest or nose.If you're doing a cross-process call then IMO they're no longer unit tests and have become integration tests. Unit tests test small pieces of code for validity and should run fast. You can't do that if you're hitting the database.
The fear of running unit tests in production is that someone screwed up the testing configuration and your database writes are no longer happening on a mocked SQLite connection, but hitting the live database. Oops!
It's better for me because it works when I'm on an airplane, that's all. If you do all of your development where the test database is accessible, then there's little to no benefit.
> The fear of running unit tests in production is that someone screwed up the testing configuration and your database writes are no longer happening on a mocked SQLite connection, but hitting the live database. Oops!
I Agree. Unit tests should never be run in or near production systems. The whole purpose of the tests is predicated on the assumption that your code is broken. Do you really want to bring broken code near customer data?
Certainly! There's a lot of code that doesn't work with persistent storage, though, or can easily have its side-effects limited to some safe space like files in /tmp.
You can just run "python setup.py test"
Is that a thing that the standard distutils package supports, or is it an extension from setuputils/distribute?
So for instance you might have some necessary build utilities, such as setup.py, a README, perhaps some tests, as well as a dir containing the actual package. Thought of like this, there's no actual redundancy, but it definitely is confusing until you make that connection.
Here's a simple example:
project/
README
setup.py
test_project.py
project/
__init__.py
project.py
Here project/project/ is the actual package, everything above that dir is simply ancillary to the actual package itself. django-useful-app/
setup.py
...
useful_app/
__init.py
.. from .context import sample
is better avoided. Why not this? from tests.context import sampleI've been mostly working with Rails lately, and I'd like to continue using tests, mocks/stubs, and sensible build rules, but I'm not sure what the preferred Python tools are. What's the best test::unit equivalent? What's the best way to mock an object? Do people really use Makefiles rather than something else (like SCons)? Is there some way to use virtualenvwrapper without bash (e.g., M-x eshell)?
For deployment I use fabric, but I have Make targets for the most used commands (again, it's nice to have completion). For example, these are two targets to deploy to my server and to my test machine:
server-deploy:
fab -f deployment/fabfile.py prod deploy
test-deploy:
fab -f deployment/fabfile.py test deploy
I use either pytest or nosetests to run my tests, mainly to have better and colored output.I don't think you can use virtualenv(wrapper) without bash, but you can use use it with M-x ansi-term. But I got tired of trying to config emacs to run Python the way I wanted and now I edit my code an emacs and run the code in IPython in the terminal. Ipython's autoreload [0] is a huge help.
[0] http://ipython.org/ipython-doc/dev/config/extensions/autorel...
export PYTHONDONTWRITEBYTECODE=1
:)For mocks, wait until mock (http://www.voidspace.org.uk/python/mock/) is in the stdlib or download it yourself.
And so on, and so forth. Please, guys, if you write a blog post, add some context!
README files are very, very old. As old as packaging software. To give you an idea of just how old they are, the file name is in all-capitals because when they were first used there were no lowercase letters in file names.
The file would be called README and it would be referred to as a 'README' in docs. We just kept it as all-uppercase with modern operating systems with lower-case characters because being uppercase kept them at the top of directory listings.
GitHub picked it up as the default description page because these files became standards in code packages, definitely didn't happen the other way around - with README becoming more popular with GitHub!
There's only one "kind" of repository generally referred to in programming topics.
> Surely, the repositories at my company don't have readmes
Good lord, why not?! Mine do!
> When you join a team, the rest of the team is the readme.
What nonsense. That is a horrible, inefficient use of resources. Stop it now, you're wasting time and, hence, money. There is no reason to keep walking new developers through the same steps and answering the same questions over and over again. Write a README! And a HACKING!
In fact, if people were to standardize their repo's folder structure to match a template, that would tell me even less about the project. Seems counterproductive to me.
another option is buildout(http://buildout.org) for more complex systems. it really makes it easy to go from clone repo, to running/testing/debugging/hacking