1
import unittest, requests, json;

class Test_interface_py_cm(unittest.TestCase):
    def setUp(self):
        print("<<<<<< start test >>>>>>")
        self.base_url = "http://192.168.11.67:8063/api/"
        self.s = requests.Session()
        self.headers = {'content-type': 'application/json;charset=UTF-8'}

    def tearDown(self):
        print("<<<<<< end test >>>>>>")

    def test1(self, arg_page, arg_pagesize):
        data = {"minTimes": 9}
        r = self.s.post(
            self.base_url + "mining/analysis/community/stranger/list/page/" + arg_page + "/pagesize/" + arg_pagesize,
            data=json.dumps(data), headers=self.headers)
        result = r.json()
        self.assertEqual(r.status_code, 200)
        self.assertEqual(result["errCode"], 0)
        self.assertEqual(result['data']['name'], '31598')
        r.connection.close()


if __name__ == '__main__':
    unittest.main()

When I want to pass parameters to the test1 method, in the main() method, how to pass parameters to test1(Must use unittest test framework to call)

cydia
  • 103
  • 12

1 Answers1

0

If you want to parameterize your test with the arg_page and arg_pagesize arguments, you could build the test methods dynamically like this.

class Test_interface_py_cm(unittest.TestCase):

    def setUp(self):
        print("<<<<<< start test >>>>>>")
        self.base_url = "http://192.168.11.67:8063/api/"
        self.s = requests.Session()
        self.headers = {'content-type': 'application/json;charset=UTF-8'}

    def tearDown(self):
        print("<<<<<< end test >>>>>>")


if __name__ == '__main__':

    def create_test(name, arg_page, arg_pagesize):
        def test1(self):
            data = {"minTimes": 9}
            r = self.s.post(
                self.base_url + "mining/analysis/community/stranger/list/page/" + arg_page + "/pagesize/" + arg_pagesize,
                data=json.dumps(data), headers=self.headers)
            result = r.json()
            self.assertEqual(r.status_code, 200)
            self.assertEqual(result["errCode"], 0)
            self.assertEqual(result['data']['name'], '31598')
            r.connection.close()
        setattr(Test_interface_py_cm, name, test1)

    create_test(name='test1', arg_page='page1', arg_pagesize=10)
    create_test(name='test2', arg_page='page1', arg_pagesize=20)
    create_test(name='test3', arg_page='page2', arg_pagesize=10)
    create_test(name='test4', arg_page='page2', arg_pagesize=20)

    unittest.main()

In the example above, the test methods are dynamically added to the test class by calling create_test. Each method runs with a different value of arg_page and arg_pagesize. The name you supply must of course be unique.

cydia
  • 103
  • 12
Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • This method can indeed pass arguments to the ```test case```, but if I have a lot of ```test cases``` and the ```test case``` arguments are all different, then do I have to encapsulate a method when passing each ```test case```? This seems to be more troublesome – cydia Mar 21 '18 at 01:47
  • Yes if you're looking to parameterize multiple test methods, then you'd need to encapsulate each in a closure like in the above example. A bit cumbersome, but given that `unittest` doesn't support passing parameters to tests, the options are rather limited. – Will Keeling Mar 21 '18 at 09:10
  • Maybe try ```@parameterized.expand``` to pass parameters – cydia Mar 22 '18 at 03:51