How Can I Run Unittest Tests In Random Order (Python 3.8)

Viewed 190

It seems tests are run in alphabetic order by default. I'm trying to run them in random order. The following shows what I have and the commented out lines at the bottom show the different commands I've tried. Nothing has worked so far. They all pass but run alphabetically each time. Thanks.

import unittest import random from appium import webdriver from PIL import Image, ImageChops

class SimpleNucleoTests(unittest.TestCase):

@classmethod
def setUpClass(self):
    # set up appium
    desired_caps = {}
    desired_caps["app"] = 'C:\\Temp\\Acist\\acist-nucleo\\AcistNucleoProtoGUI.exe'
    desired_caps['platformName'] = 'Windows'
    desired_caps['deviceName'] = 'WindowsPC'
    self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

@classmethod
def tearDownClass(self):
    self.driver.quit()

def test_defaultLayout(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.driver.get_screenshot_as_file('C:\\Temp\\ss_new.png')
    new_image = Image.open("C:\\temp\\ss_new.png")
    orig_image = Image.open("C:\\temp\\ss_orig.png")
    diff = ImageChops.difference(new_image, orig_image)
    if diff.getbbox():
        diff.show()
    else:
         print("Images are the same")
    self.assertEqual(diff.getbbox(), None)

def getFlowRate(self):
    displaytext = self.driver.find_element_by_accessibility_id("FlowRateField").find_element_by_accessibility_id("InputTextBox").text
    return displaytext

def test_defaultFlowRate(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.assertEqual(self.getFlowRate(), "4.0")

def test_flowRateUp(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.driver.find_element_by_accessibility_id("UpButton").click()
    self.assertEqual(self.getFlowRate(), "4.5")

def test_flowRateDown(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.driver.find_element_by_accessibility_id("DownButton").click()
    self.assertEqual(self.getFlowRate(), "3.5")

def getVolume(self):
    displaytext = self.driver.find_element_by_accessibility_id("VolumeField").find_element_by_accessibility_id("InputTextBox").text
    return displaytext

def test_defaultVolume(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.assertEqual(self.getVolume(), "6.0")

def test_volumeUp(self):
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.driver.find_element_by_accessibility_id("VolumeField").find_element_by_accessibility_id("UpButton").click()
    self.assertEqual(self.getVolume(), "6.5")

def test_volumeDown(self):
    print("Starting test 'volumeDown'")
    self.driver.find_element_by_accessibility_id("InjectorButton").click()
    self.driver.find_element_by_accessibility_id("VolumeField").find_element_by_accessibility_id("DownButton").click()
    self.assertEqual(self.getVolume(), "5.5")

if name == 'main': # unittest.TestLoader().main

suite = unittest.TestLoader().sortTestMethodsUsing = None
unittest.TextTestRunner(verbosity=2).run(suite)

# unittest.TestLoader().sortTestMethodsUsing = None

# unittest.TestLoader.sortTestMethodsUsing = lambda self, a, b: random.choice([1, 0, -1])

# unittest.TestLoader.sortTestMethodsUsing = lambda _, x, y: cmp(y, x)

# unittest.TextTestRunner(verbosity=2).main()

# unittest.main(verbosity=2)
0 Answers
Related