2

How do I pass variables declared from the python command line to the testcase instance? Edit: a and b are inputs to the method func().

a = [1,2,3]
b = np.array([1,2])

Filename: code.py

import unittest
import numpy as np

def func(a,b)
    c = a*b
    return (c)

class TestCases(unittest.TestCase):
    def test_length_a_equals_length_b(self):
        self.assertEqual(len(a), len(b), msg="len(a) != len(b)")

How would I input a and b into the test case, so an error occurs when they are not the same length?

I get the following error when I run the file from terminal:

ERROR: test_a_len_equals_len_b (main.TestCases)  
---------------------------------------------------------------------- 
Traceback (most recent call last): 
 File "code.py", in test_length_a_equals_length_b 
   self.assertEqual(len(a), len(b), msg="len(a) != len(b)") 
NameError: global name 'a' is not defined
MyopicVisage
  • 1,333
  • 1
  • 19
  • 33

1 Answers1

5

Instead of using assertNotEqual, use assertEqual. This will fail when a and b aren't the same length.

Edit to answer the comment: Where are 'a' and 'b' defined? You could just define them in your module scope (just in the begining of code.py file) or you could define then in your test method, like this:

def test_length_a_equals_length_b(self):
    a = [1,2,3]
    b = np.array([1,2])
    self.assertEqual(len(a), len(b), msg="len(a) != len(b)")
Juca
  • 479
  • 3
  • 5
  • thanks, you are correct, but my problem is: ERROR: test_a_len_equals_len_b (__main__.TestCases) ---------------------------------------------------------------------- Traceback (most recent call last): File "code.py", in test_length_a_equals_length_b self.assertNotEqual(len(a), len(b), msg="len(a) != len(b)") NameError: global name 'a' is not defined – MyopicVisage Mar 10 '15 at 20:43
  • Where are 'a' and 'b' defined? You could just define them in your module scope (just in the begining of code.py file) or you could define then in your test method, like this: def test_length_a_equals_length_b(self): a = [1,2,3] b = np.array([1,2]) self.assertEqual(len(a), len(b), msg="len(a) != len(b)") – Juca Mar 10 '15 at 20:51
  • I expect to use this file as a module, so the a and b variables would be define externally. Is this beyond the scope of a test_case? Should all test cases contain their input variables? – MyopicVisage Mar 10 '15 at 20:57
  • No, the variables being created on test case was only a example. As the variables are being defined externally you must import them first. Something like: from myprogram.mymodule import a, b – Juca Mar 10 '15 at 21:00
  • I was hoping to: import code.py into another file and use the test case to error when func(a,b) had arrays that were not of the same length. BUT, this may not be within the scope of a test case. – MyopicVisage Mar 10 '15 at 21:02
  • Yeah, maybe... But if you really need this assertion in your code, you could write a little function: https://gist.github.com/anonymous/a634920321fe8da8968d and import and use this function where you need. – Juca Mar 10 '15 at 21:32