Skip to main content

All Questions

Tagged with
851 votes
14 answers
723k views

How do I properly assert that an exception gets raised in pytest?

Code: # coding=utf-8 import pytest def whatever(): return 9/0 def test_whatever(): try: whatever() except ZeroDivisionError as exc: pytest.fail(exc, pytrace=True) Output:...
Gill Bates's user avatar
  • 15.1k
559 votes
7 answers
266k views

Writing unit tests in Python: How do I start? [closed]

I completed my first proper project in Python and now my task is to write tests for it. Since this is the first time I did a project, this is the first time I would be writing tests for it. The ...
user225312's user avatar
  • 130k
486 votes
18 answers
434k views

How do I run all Python unit tests in a directory?

I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py. I am attempting to make a file called all_test.py that will, you guessed it, run all files in the ...
Stephen Cagle's user avatar
266 votes
22 answers
209k views

Trying to mock datetime.date.today(), but not working

Can anyone tell me why this isn't working? >>> import mock >>> @mock.patch('datetime.date.today') ... def today(cls): ... return date(2010, 1, 1) ... >>> from datetime ...
Belmin Fernandez's user avatar
265 votes
10 answers
246k views

How do I disable a test using pytest?

Let's say I have a bunch of tests: def test_func_one(): ... def test_func_two(): ... def test_func_three(): ... Is there a decorator or something similar that I could add to the ...
ericmjl's user avatar
  • 14.3k
253 votes
9 answers
371k views

How can I time a code segment for testing performance with Pythons timeit?

I've a python script which works just as it should, but I need to write the execution time. I've googled that I should use timeit but I can't seem to get it to work. My Python script looks like this: ...
Emil Devantie Brockdorff's user avatar
196 votes
16 answers
87k views

Python unit test with base and sub class

I currently have a few unit tests which share a common set of tests. Here's an example: import unittest class BaseTest(unittest.TestCase): def testCommon(self): print 'Calling BaseTest:...
Thierry Lam's user avatar
190 votes
8 answers
216k views

Run code before and after each test in py.test?

I want to run additional setup and teardown checks before and after each test in my test suite. I've looked at fixtures but not sure on whether they are the correct approach. I need to run the setup ...
edA-qa mort-ora-y's user avatar
156 votes
8 answers
281k views

How to send mail with To, CC and BCC?

I need to populate a few hundred email boxes with various messages for testing purposes and I was going to use smtplib for that. I need to send messages not only To specific mailboxes, but CC and BCC ...
user63503's user avatar
  • 6,433
150 votes
10 answers
199k views

How to pass arguments in pytest by command line

I have a code and I need to pass the arguments like name from terminal. Here is my code and how to pass the arguments. I am getting a "File not found" kind error that I don't understand. I have tried ...
ashish sarkar's user avatar
134 votes
10 answers
47k views

Practicing BDD with python [closed]

Which are the most advanced frameworks and tools there are available for python for practicing Behavior Driven Development? Especially finding similar tools as rspec and mocha for ruby would be great.
JtR's user avatar
  • 20.7k
132 votes
6 answers
77k views

Is it possible exclude test directories from coverage.py reports?

I'm kind of a rookie with python unit testing, and particularly coverage.py. Is it desirable to have coverage reports include the coverage of your actual test files? Here's a screenshot of my HTML ...
Kyle Fox's user avatar
  • 3,233
123 votes
4 answers
135k views

Mocking two functions with patch for a unit test

I have a function I want to unit test contains calls two other functions. I am unsure how can I mock both functions at the same time properly using patch. I have provided an example of what I mean ...
cnodell's user avatar
  • 1,301
115 votes
3 answers
154k views

Mock attributes in Python mock?

I'm having a fairly difficult time using mock in Python: def method_under_test(): r = requests.post("http://localhost/post") print r.ok # prints "<MagicMock name='post().ok' id='11111111'&...
Naftuli Kay's user avatar
  • 90.4k
107 votes
10 answers
69k views

Unit tests for functions in a Jupyter notebook?

I have a Jupyter notebook that I plan to run repeatedly. It has functions in it, the structure of the code is this: def construct_url(data): ... return url def scrape_url(url): ... # ...
Richard's user avatar
  • 64.9k

15 30 50 per page
1
2 3 4 5
237