python-unittest

Attempted relative import beyond toplevel package

安稳与你 提交于 2020-01-20 02:16:00
问题 Here is my folder structure: Mopy/ # no init.py ! bash/ __init__.py bash.py # <--- Edit: yep there is such a module too bass.py bosh/ __init__.py # contains from .. import bass bsa_files.py ... test_bash\ __init__.py # code below test_bosh\ __init__.py test_bsa_files.py In test_bash\__init__.py I have: import sys from os.path import dirname, abspath, join, sep mopy = dirname(dirname(abspath(__file__))) assert mopy.split(sep)[-1].lower() == 'mopy' sys.path.append(mopy) print 'Mopy folder

How can I ignore certain values when comparing dictionaries in unittest?

左心房为你撑大大i 提交于 2020-01-15 11:45:07
问题 I would like to assert that two dictionaries are equal, using Python's unittest, but ignoring the values of certain keys in the dictionary, in a convenient syntax, like this: from unittest import TestCase class Example(TestCase): def test_example(self): result = foobar() self.assertEqual( result, { "name": "John Smith", "year_of_birth": 1980, "image_url": ignore(), # how to do this? "unique_id": ignore(), # }, ) To be clear, I want to check that all four keys exist, and I want to check the

Python: How to capture the stdout/stderr of a unittest in a variable?

强颜欢笑 提交于 2020-01-15 01:20:51
问题 How to capture the stdout/stderr of a unittest in a variable? I need to capture the entire output output of the following unit test and send it to SQS. I have tried this: import unittest, io from contextlib import redirect_stdout, redirect_stderr class LogProcessorTests(unittest.TestCase): def setUp(self): self.var = 'this value' def test_var_value(self): with io.StringIO() as buf, redirect_stderr(buf): print('Running LogProcessor tests...') print('Inside test_var_value') self.assertEqual

Python: How to capture the stdout/stderr of a unittest in a variable?

为君一笑 提交于 2020-01-15 01:20:11
问题 How to capture the stdout/stderr of a unittest in a variable? I need to capture the entire output output of the following unit test and send it to SQS. I have tried this: import unittest, io from contextlib import redirect_stdout, redirect_stderr class LogProcessorTests(unittest.TestCase): def setUp(self): self.var = 'this value' def test_var_value(self): with io.StringIO() as buf, redirect_stderr(buf): print('Running LogProcessor tests...') print('Inside test_var_value') self.assertEqual

Python: How to capture the stdout/stderr of a unittest in a variable?

孤者浪人 提交于 2020-01-15 01:20:09
问题 How to capture the stdout/stderr of a unittest in a variable? I need to capture the entire output output of the following unit test and send it to SQS. I have tried this: import unittest, io from contextlib import redirect_stdout, redirect_stderr class LogProcessorTests(unittest.TestCase): def setUp(self): self.var = 'this value' def test_var_value(self): with io.StringIO() as buf, redirect_stderr(buf): print('Running LogProcessor tests...') print('Inside test_var_value') self.assertEqual

Import error running unittest in Python3

主宰稳场 提交于 2020-01-14 22:53:29
问题 I have a problem importing files in Python 3.6. My directories tree is as given below: project/ app/ ├── __init__.py ├── a.py └── b.py test/ ├── __init__.py ├── test_a.py └── test_b.py It works my application (but, no works the tests) using following import statement in b.py : from a import * But, it does not work my application (but, works the tests) using this other in b.py : from .a import * So, I choose from a import * . Executing test like python3 -m unittest I always get following error

How to capture screenshot on test case failure for Python Unittest

廉价感情. 提交于 2020-01-14 20:08:51
问题 I am using Python 3.6.5 with the following libraries: Appium-Python-Client==0.26 unittest2==1.1.0 selenium==3.5.0 pytest==3.6.3 Now I need to capture the screenshot in case of test failure, So intentionally I did put a false statement self.driver.find_element_by_css_selector('test') . I am using sys.exc_info() . But when I executing below code using a command: py.test untitled.py or python3 -m unittest untitled.py it does not capturing it. Code: import sys, time, unittest2 from selenium

How to capture screenshot on test case failure for Python Unittest

邮差的信 提交于 2020-01-14 20:08:13
问题 I am using Python 3.6.5 with the following libraries: Appium-Python-Client==0.26 unittest2==1.1.0 selenium==3.5.0 pytest==3.6.3 Now I need to capture the screenshot in case of test failure, So intentionally I did put a false statement self.driver.find_element_by_css_selector('test') . I am using sys.exc_info() . But when I executing below code using a command: py.test untitled.py or python3 -m unittest untitled.py it does not capturing it. Code: import sys, time, unittest2 from selenium

Python pre-commit unittest faild

可紊 提交于 2020-01-14 02:39:19
问题 I wish pre-commit to run the tests before committing my code. The command python -m unittest discover is working in the command line. D:\project_dir>python -m unittest discover ... ... ... ---------------------------------------------------------------------- Ran 5 tests in 6.743s OK But when trying to commit I am getting D:\project_dir>git commit -m "fix tests with hook" run tests................................................................Failed hookid: tests usage: python.exe -m

Disable individual Python unit tests temporarily

孤街浪徒 提交于 2020-01-11 15:09:19
问题 How can individual unit tests be temporarily disabled when using the unittest module in Python? 回答1: Individual test methods or classes can both be disabled using the unittest.skip decorator. @unittest.skip("reason for skipping") def test_foo(): print('This is foo test case.') @unittest.skip # no reason needed def test_bar(): print('This is bar test case.') For other options, see the docs for Skipping tests and expected failures. 回答2: You can use decorators to disable the test that can wrap