python-unittest

python-mock: 'self' parameter lacking default value

旧城冷巷雨未停 提交于 2019-12-13 16:09:45
问题 This used to work with python mock version 1.0.1, but started failing after I upgraded to mock version 1.3.0. I am running python version 2.7.10 on Mac OS X Yosemite 10.10.5. I reduced the logic from an existing production test to the following dummy test that reproduces the issue: import unittest import mock from mock import Mock, patch class Outer(object): class MyClass(object): def doStuff(self, action): pass @patch.object(Outer, "MyClass", autospec=True, return_value=Mock(spec_set=Outer

defining a one-off class within python unittest

老子叫甜甜 提交于 2019-12-13 04:23:59
问题 Feel free to close if this is a duplicate, but I haven't seen anything similar as of yet. I'm from more of a "classical OOP" background (e.g. Java, C#, C++, PHP (if you want to call that an OOP language...) etc), and I'm trying to master Python right now. Let's say we have a class defined like this: class Foo: def __init__(self): self.stuff = self.defineStuff() def method1(self, x): return x def defineStuff(self): # this method has no logic in this class; child classes should define this pass

Pyunit framework extending the classes of unittest

♀尐吖头ヾ 提交于 2019-12-13 02:50:04
问题 In Pyunit framework, I have question as below: import unittest class xyz(object): def test_fuc(self): print "test_fun" pass class abc(unittest.Testcase, xyz): def setUp(self): print "setUp" def tearDown(self): print "tearDown" def test_one(self): print "test_one" pass def test_two(self): print "test_two" pass Output: test_one test_two test_fun but I need output like below: test_one test_fun test_two test_fun Please suggest, How do I do it? 回答1: Unit tests (ie. unittest.TestCase methods) are

Writing a Python functional test which requires a username and password

ε祈祈猫儿з 提交于 2019-12-12 23:06:42
问题 I have an API wrapper class WfcAPI written in Python 3 which I want to test using PyUnit. The setUpClass() for WfcAPI involves logging in to the external API server. The current functional test implementation has the password obfuscated with Base64 encoding, but this is far from an ideal solution for security reasons. import unittest import base64 from pykronos import WfcAPI class Test(unittest.TestCase): @classmethod def setUpClass(self): password = base64.b64decode("U29tZVBhc3N3b3Jk")

Unit testing entire project hierarchy in Python using unittest in pydev

与世无争的帅哥 提交于 2019-12-12 11:25:09
问题 I am using unittest module to unit test some python code that has been created in a hierarchical struture of packages using Pydev. The problem arises as I try to use separate source folders for actual source code and its unit test in pydev. project |----src | |----com | | |----myself | | | |----MyApplication | | | | |----SampleFileToTest.py => The application file that I want to test |----test | |----com | | |----myself | | | |----MyApplication | | | | |----TestTheSampleFileToTest.py => My

Why do I get an AttributeError with Python3.4's `unittest` library?

血红的双手。 提交于 2019-12-12 03:47:42
问题 So here is my code: import unittest class Primes: @staticmethod def first(n): <python code here> class Test(unittest.TestCase): def __init__(self): pass def assert_equals(self, l, m): self.assertEqual(l, m) Test = Test() Test.assert_equals(Primes.first(1), [2]) Whenever I run my code, I get the following error: Traceback (most recent call last): File "firstNPrimes.py", line 37, in <module> Test.assert_equals(Primes.first(1), [2]) File "firstNPrimes.py", line 34, in assert_equals self

How can generate automatically test case number in unittest?

点点圈 提交于 2019-12-11 16:46:45
问题 how can I generate automatically the numbers of test cases in unittest? I mean something like test_01, test_02, test_{generate number}. import unittest class TestSum(unittest.TestCase): def test_01_sum(self): self.assertEqual(sum([1, 2, 3]), 6, "Should be 6") def test_02_sum_tuple(self): self.assertEqual(sum((1, 2, 2)), 6, "Should be 6") if name == '__main__': unittest.main() 回答1: To achieve this at runtime, you can actually rename the test methods for your test class: def generate_test

How to define test methods using Python Unittest

馋奶兔 提交于 2019-12-11 15:44:59
问题 import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class CorrecaoEfetivaNota(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver') def teste_login_avaliador(self): driver = self.driver driver.get("") cpf = driver.find_element_by_xpath('//input[@placeholder="CPF"]') cpf.send_keys("") password = driver.find_element_by_xpath('//input[@placeholder="SENHA"]') password.send_keys("") login = driver.find_element

How to set “return_value” once for TestCase. Python. Django

情到浓时终转凉″ 提交于 2019-12-11 13:45:12
问题 Here is example test: import a import b import c import mock from django.test import TestCase @mock.patch.object(a, "method_a") @mock.patch.object(b, "method_b") @mock.patch.object(c, "method_c") class SomeTestCase(TestCase): def setUp(self): # I want to set mock_method_a.return_value = 1 once here (or not here, but once) pass def test_one(self, mock_method_a, mock_method_b, mock_method_c): mock_method_a.return_value = 1 mock_method_b.return_value = 2 pass # some test stuff def test_two(self,

python - unittesting super() calls

谁都会走 提交于 2019-12-11 09:03:09
问题 class A(object): def get_value(self): return "foo" class B(A): def get_value(self): value = super(B, self).get_value() value + "bar" return value Given the above classes, when I wanted to write a testsuite for the B() class, how do I go about mocking that super() call to A() 's get_value() method? The pseudo-ish code for what I want would be: class BTestCase(TestCase): def setUp(self): self.b = B() def test_get_value(self): # This won't work, attribute error- "super" object has # no attribute