How do I run the test after completing all the tests? ( - python

There are several tests in the tests folder: test_first.py , test_second.py .
Each test checks the operation of the site by creating and modifying projects. I need that, regardless of how I run the tests tests/test_first.py or tests/ upon completion of all tests, the delete_prj function is executed, which deletes the created project after the tests have run. I'm not asking for the task execution code, the answer to the question is enough

Use a session scoped fixture. Something like this:
import pytest
#pytest.fixture(autouse=True, scope="session")
def do_stuff_after_everything_is_done():
yield
delete_prj()
(untested)

Related

pytest-xdist: How to execute a method (where I initialize DB mocks for all the tests in that file) before the test methods are executed

I have a test suite that used to be executed with pytest and I used the method before_all_tests(request) in each test file to initialize the db mockups for those tests.
I wanted to use pytest-xdist to run them parallelly, but before_all_tests(request) is not being executed if I run pytest -n X (parallelly).
Now I don't know how to re-write all my code so that I can only initialize the mocks of the tests being executed :(
I use markers to categorize tests, so sometimes I only run tests under a single category or just a bunch of them. I don't want to initialize the mocks of all the tests files because not all of them will be executed at that time.
Can anybody help me? I can post some code if necessary.
Thanks in advance!!

running a pytest hook only once on the start of the tests

I'm having a pytest run that invokes N tests. I want all tests to output into the same log, and I want to clear the log before pytest is triggered, so different runs would not contiminate each other. So far, I tried adding my clear_log() to some fixtures or hooks, but none has achieved what In wanted - or they were called each time (pytest_runtest_makereport for example), or they were not called at all (some function that was decorated with pytest.fixture()
I also tried having a global flag that would indicate if this is the first run, but it didn't work either...
Where can I put the function so it would be invoked only once?
Following decorator allows you to execute your fixture only once (even if there are multiple number of tests are running):
#pytest.fixture(scope="session")
More details here

Tornado: Reset database before running all tests

I am using Python Tornado web server. When I write test, before all tests, I want to do something (such as prepare some data, reset database ...). How can I achieve this in Python or Tornado web server.
In some languages I can easily do this. Example: in Golang, there is a file named main_test.go.
Thanks
In your test folder, you create __init__.py and initialize everything here.
// __init__.py
reset_database()
run_migration()
seed_data()
Noted that, you should configure your project running test from root folder. For example, if your test inside app/tests/api/sample_api.py, your test should be run from app. In this case, __init__.py will always run before running your sample_api.py. Here is the command line I usually run for running all tests inside project:
python -m unittest discover
If you use unittest.TestCase or tornado.testing.*TestCase (which actually are subclasses of unittest.TestCase), look at the setUp() and tearDown() methods. You can wrap everything you want just like
class MyTests(unittest.TestCase):
def setUp(self):
load_data_to_db()
def test_smth(self):
self.assertIsInstance("setUp and tearDown are useful", str)
def tearDown(self):
cleanup_db()

How to a run specific code before & after each unit test in Python

Following is the structure of my tests in a file.
Class
 setup
  test01
  test02
  test03
 teardown
I have a requirement to run specific code before and after each test.
For before, I could invoke that code from the setup.
But for after the test, I am not able to figure how to do it.
Obviously invoking the code from teardown would work for the last test, but how can I have it run for the tests in between?
Assuming that you're properly using a class descended from unittest.TestCase, then the setUp method is run before each test, and the tearDown method is run after each test. Check the documentation. So it's completely feasible to put your code in those two methods.

Execute Django Testcase Suite Manually

Scenario:
Suppose I have some python scripts that do maintenance on the DB, if I use unittest to run tests on those scripts then they will interact with the DB and clobber it. So I'm trying to find a way to use the native Django test suite which simulates the DB without clobbering the real one. From the docs it looks like you can run test scripts using manage.py tests /path/to/someTests.py but only on Django > v1.6 (I'm on Django-Nonrel v1.5.5).
I found How do I run a django TestCase manually / against other database, which talks about running individual test cases and it seems dated--it mentions Django v1.2. However, I'd like to manually kick off the entire suite of tests I've defined. Assume for now that I can't kick off the suite using manage.py test myApp (because that's not what I'm doing). I tried kicking off the tests using unittest.main(). This works with one drawback... it completely clobbers the database. Thank goodness for backups.
someTest.py
import unittest
import myModule
from django.test import TestCase
class venvTest(TestCase): # some test
def test_outside_venv(self):
self.failUnlessRaises(EnvironmentError, myModule.main)
if __name__ == '__main__':
unittest.main() # this fires off all tests, with nasty side effect of
# totally clobbering the DB
How can I run python someTest.py and get it to fire off all the testcases using the Django test runner?
I can't even get this to work:
>>> venvTest.run()
Update
Since it's been mentioned in the comments, I asked a similar but different question here: django testing external script. That question concerns using manage.py test /someTestScript.py. This question concerns firing off test cases or a test suite separately from manage.py.

Categories

Resources