How to use Python 3.6 interpreter inside a bash script?

10,219

First, install it for your python3 (if you have it and pip installed) sudo python3 -m pip install coverage

Then, in order to run coverage for python3, run python3 -m coverage report -m

So your final script should look like this:

#!/bin/bash 
python3 -m coverage run -m unittest discover 
python3 -m coverage report -m 

Also you can replace python3 with path to your pythons bin. For example /usr/bin/python3. So You can call it this way as well:

#!/bin/bash 
/usr/bin/python3 -m coverage run -m unittest discover 
/usr/bin/python3 -m coverage report -m 
Share:
10,219
Olga Zhukova
Author by

Olga Zhukova

BY DAY: trying to get an education in computer science. BY NIGHT: trying to get a good sleep after trying to get an education in computer science. EVERY MOMENT: want to become a better person (:

Updated on August 21, 2022

Comments

  • Olga Zhukova
    Olga Zhukova over 1 year

    I want to have a little script, that will find, run and report about all the tests in the folder, like this one:

    #!/bin/bash 
    coverage run -m unittest discover 
    coverage report -m 
    

    But, when I run it, I get some errors, which I do not get on Windows (like using of super() without an argument). As I've understood, it's connected with the fact, that build-in and default version of Python on Linux is 2.x, whereas I am using 3.6. How should I change the script, so it would use Python 3.6 interpreter?

    EDIT: So here's one of the files with tests that I run:

    #!/usr/bin/env python3
    import unittest
    import random
    import math
    import sort_functions as s
    from comparison_functions import less, greater
    
    
    class BaseTestCases:
        class BaseTest(unittest.TestCase):
            sort_func = None
    
            def setUp(self):
                self.array_one = [101, -12, 99, 3, 2, 1]
                self.array_two = [random.random() for _ in range(100)]
                self.array_three = [random.random() for _ in range(500)]
                self.result_one = sorted(self.array_one)
                self.result_two = sorted(self.array_two)
                self.result_three = sorted(self.array_three)
    
            def tearDown(self):
                less.calls = 0
                greater.calls = 0
    
            def test_sort(self):
                result_one = self.sort_func(self.array_one)
                result_two = self.sort_func(self.array_two)
                result_three = self.sort_func(self.array_three)
                self.assertEqual(self.result_one, result_one)
                self.assertEqual(self.result_two, result_two)
                self.assertEqual(self.result_three, result_three)
    
        # and some more tests here       
    
    
    class TestBubble(BaseTestCases.BaseTest):
        def setUp(self):
        self.sort_func = s.bubble_sort
        super().setUp()
    
    # and some more classes looking like this
    

    And the error:

    ERROR: test_key (test_sort_func.TestBubble)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "/home/lelik/Desktop/Sorters/test_sort_func.py", line 67, in setUp
    super().setUp()
    TypeError: super() takes at least 1 argument (0 given)