0

For some reason I'm having issues trying to use command line arguments with my unittests.

Simply put, all I want is to use env with all my tests. What am I doing wrong here?

# -*- coding: utf-8 -*-
import argparse
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import unittest, time, re

class mySmokeTest(unittest.TestCase):
    def __init__(self, args):
        self.env = args.env

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.base_url = "http://google.com"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_add_to_cart(self, env):
        # open base page
        driver = self.driver                    
        driver.get("{0}".format(env))

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('env', default='environment to test against')
    args = parser.parse_args()
    env = args.env
    runner = unittest.TextTestRunner()
    itersuite = unittest.TestLoader().loadTestsFromTestCase(mySmokeTest)
    runner.run(itersuite)

error:

self.env = args.env
AttributeError: 'str' object has no attribute 'env'
david
  • 6,303
  • 16
  • 54
  • 91
  • 2
    I don't know what you're doing wrong, you tell us. You need to reread the [mcve] page, specifically the part about the error. – Morgan Thrapp May 12 '16 at 18:14
  • So I am passing the argument correctly? – david May 12 '16 at 18:51
  • I don't know, are you getting any errors? – Morgan Thrapp May 12 '16 at 18:52
  • yea i updated with error – david May 12 '16 at 18:54
  • Well, you're not ever passing `args` to `mySmokeTest`. I'm not super familiar with `unittest`, but I would imagine there's a way to pass parameters into the test cases. You could also just parse the args right in the class. – Morgan Thrapp May 12 '16 at 18:58
  • I passed args to the constructor – david May 12 '16 at 19:06
  • You're *defining* `__init__` to take arguments; you aren't actually *passing* any though, or rather, something is passing a string when the class is instantiated. (I'd post an answer, but I don't know how to pass them yet either.) – chepner May 12 '16 at 19:21
  • Probable duplicate of http://stackoverflow.com/questions/1842168/python-unit-test-pass-command-line-arguments-to-setup-of-unittest-testcase. (I'd vote to close, but I'm not sure enough to use the dupe hammer here.) – chepner May 12 '16 at 19:28
  • I was able to successfully use the arg in my tests, I posted what I did below. – david May 12 '16 at 19:40

1 Answers1

0

I don't actually know why... But changing main to this works:

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('env', default='url to test against')
    args = parser.parse_args()
    env=args.env
    mySmokeTest.env = env
    runner = unittest.TextTestRunner()
    itersuite = unittest.TestLoader().loadTestsFromTestCase(mySmokeTest)
    runner.run(itersuite)

note that:

    mySmokeTest.env = args.env does not work
david
  • 6,303
  • 16
  • 54
  • 91