python-unittest

mock/patch os.path.exists with multiple return values

廉价感情. 提交于 2019-12-23 10:41:09
问题 I'm trying to test a function that I made that iterates through a list, and calls os.path.exists for each item in the list. My test is passing the function a list of 2 objects. I need os.path.exists to return True for one of them and False for the other. I have tried this: import mock import os import unittest class TestClass(unittest.TestCase): values = {1 : True, 2 : False} def side_effect(arg): return values[arg] def testFunction(self): with mock.patch('os.path.exists') as m: m.return

Incremental code coverage for Python unit tests?

大憨熊 提交于 2019-12-23 09:57:14
问题 How can I get an incremental report on code coverage in Python? By "incremental", I mean what has been the change in the covered lines since some "last" report, or from a particular Git commit. I'm using unittest and coverage (and coveralls.io) to get the code coverage statistics, which work great. But I'm involved only with a part of the project, and at first I'm concerned with what my last commit has changed. I expected coverage to be able to show the difference between two reports, but so

Incremental code coverage for Python unit tests?

可紊 提交于 2019-12-23 09:55:31
问题 How can I get an incremental report on code coverage in Python? By "incremental", I mean what has been the change in the covered lines since some "last" report, or from a particular Git commit. I'm using unittest and coverage (and coveralls.io) to get the code coverage statistics, which work great. But I'm involved only with a part of the project, and at first I'm concerned with what my last commit has changed. I expected coverage to be able to show the difference between two reports, but so

Run a specific unit test function inside PyCharm IDE 5.0.4

99封情书 提交于 2019-12-23 06:48:09
问题 I am trying to use PyCharm for unit testing (with unittest ), and am able to make it work: the test runner nicely shows the list of test cases and nested test functions. However, once the tests have been discovered, I cannot find any way to (re)run a specific test function: the only button available will run the whole list of tests, and right clicking on a single test function doesn't show any meaningful action for this purpose. As you can imagine, it can take a long time unnecessarily when

Django test tables are not being created

走远了吗. 提交于 2019-12-22 23:21:40
问题 I'm trying to write test cases for my django project but when I run "$ ./manage.py test" command its creating test database but its not creating any tables and I'm getting an error that table does't exists. Any suggestions are welcome. Here is my model which i have created through "./manage.py inspectdb > models.py" class MyCustomModel(models.Model): name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) class Meta: managed = False db_table = 'MY_TABLE' 回答1: Your

if condition in setUp() ignore test

為{幸葍}努か 提交于 2019-12-22 14:39:09
问题 in unittest python library, exists the functions setUp and tearDown for set variables and other things pre and post tests. how I can run or ignore a test with a condition in setUp ? 回答1: You can call if cond: self.skipTest('reason') in setUp() . 回答2: Instead of checking in setUp , use the skipIf decorator. @unittest.skipIf(not os.path.exists("somefile.txt"), "somefile.txt is missing") def test_thing_requiring_somefile(self): ... skipIf can also be used on the class, so you can skip all

How to mock data as request.Response type in python

我的梦境 提交于 2019-12-22 09:47:12
问题 I would like to write some testcase to exercise object_check in isinstance(obj, requests.Response) logic. After I create Mock data as return value for requests.post. The type for mock data is always be Mock class. In that way, how can I rewrite mock data so mock data can be type of requests.Response? so I can exercise line d = obj.json() ? from unittest.mock import patch, Mock import unittest import requests from requests.exceptions import HTTPError import pytest def object_check(obj): if

Initializing MEDIA_ROOT before each Django Test

自作多情 提交于 2019-12-22 07:59:43
问题 I want to my Django tests to create and modify media files. So, much like Django tests do with databases, I want to set up an empty MEDIA_ROOT folder before each test is run. I figured I'll create a temporary folder and point MEDIA_ROOT to it. However, I can't figure out where to put the code that does this. In this example, a special Runner is created. The runner sets up the media root and tears it down. Unfortunately, setup_test_environment is called once before the first test function is

Initializing MEDIA_ROOT before each Django Test

落花浮王杯 提交于 2019-12-22 07:59:11
问题 I want to my Django tests to create and modify media files. So, much like Django tests do with databases, I want to set up an empty MEDIA_ROOT folder before each test is run. I figured I'll create a temporary folder and point MEDIA_ROOT to it. However, I can't figure out where to put the code that does this. In this example, a special Runner is created. The runner sets up the media root and tears it down. Unfortunately, setup_test_environment is called once before the first test function is

Initializing MEDIA_ROOT before each Django Test

被刻印的时光 ゝ 提交于 2019-12-22 07:59:04
问题 I want to my Django tests to create and modify media files. So, much like Django tests do with databases, I want to set up an empty MEDIA_ROOT folder before each test is run. I figured I'll create a temporary folder and point MEDIA_ROOT to it. However, I can't figure out where to put the code that does this. In this example, a special Runner is created. The runner sets up the media root and tears it down. Unfortunately, setup_test_environment is called once before the first test function is