How to run unit tests from inside a python file

Viewed 17

Problem: I can't figure out how to run my unittests from within my running application.

The Goal: I want to run my unit tests before this scraper function. If the tests pass, we move on. If they fail, we do nothing. Nothing here is better than broken.

Inside a py file, every time I call the scraper I want the tests to run first, something like:

@ app.route('/scrape')
def scraper():
    #  call the unit tests here
    #  somehow get a return value. I don't know how to do this either.
   # if return value == True then tests pass, so run:
    scraper.scrape()
    return 

What I've tried:

  1. I tried running this script inside that scraper func above, like a bash script, based on learnings from SO:

    subprocess.call(["python - m unittest", "test_app.TestScraper.test_driver_scraper"])

Could just be a syntax error. But can you even call a python script inside a running python file?

  1. Also tried to import unittest and call run on it inside scrape; this based on other learnings. Also didn't work. This isn't a unittest thing.

Question: So how can I run my unit tests from inside this python file?

Note: The fact that I could not find any info on how to do this might mean it's uncommon, and there's maybe a better way to do it?

0 Answers
Related