How to pass arguments in pytest by command line

In your pytest test, don’t use @pytest.mark.parametrize: def test_print_name(name): print (“Displaying name: %s” % name) In conftest.py: def pytest_addoption(parser): parser.addoption(“–name”, action=”store”, default=”default name”) def pytest_generate_tests(metafunc): # This is called for every test. Only get/set command line arguments # if the argument is specified in the list of test “fixturenames”. option_value = metafunc.config.option.name if ‘name’ in … Read more

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

Another option is to use https://github.com/spulec/freezegun/ Install it: pip install freezegun And use it: from freezegun import freeze_time @freeze_time(“2012-01-01”) def test_something(): from datetime import datetime print(datetime.now()) # 2012-01-01 00:00:00 from datetime import date print(date.today()) # 2012-01-01 It also affects other datetime calls in method calls from other modules: other_module.py: from datetime import datetime def other_method(): … Read more

Sharing Test code in Maven

I recommend using type instead of classifier (see also: classifier). It tells Maven a bit more explicitly what you are doing (and I’ve found that m2eclipse and q4e both like it better). <dependency> <groupId>com.myco.app</groupId> <artifactId>foo</artifactId> <version>1.0-SNAPSHOT</version> <type>test-jar</type> <scope>test</scope> </dependency>

What’s the difference between a mock & stub?

Foreword There are several definitions of objects, that are not real. The general term is test double. This term encompasses: dummy, fake, stub, mock. Reference According to Martin Fowler’s article: Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. Fake objects actually have working implementations, but … Read more

What’s the difference between unit, functional, acceptance, and integration tests? [closed]

Depending on where you look, you’ll get slightly different answers. I’ve read about the subject a lot, and here’s my distillation; again, these are slightly wooly and others may disagree. Unit Tests Tests the smallest unit of functionality, typically a method/function (e.g. given a class with a particular state, calling x method on the class … Read more

How to mock private method for testing using PowerMock?

I don’t see a problem here. With the following code using the Mockito API, I managed to do just that : public class CodeWithPrivateMethod { public void meaningfulPublicApi() { if (doTheGamble(“Whatever”, 1 << 3)) { throw new RuntimeException(“boom”); } } private boolean doTheGamble(String whatever, int binary) { Random random = new Random(System.nanoTime()); boolean gamble = … Read more

How to Re-run failed JUnit tests immediately?

You can do this with a TestRule. This will give you the flexibility you need. A TestRule allows you to insert logic around the test, so you would implement the retry loop: public class RetryTest { public class Retry implements TestRule { private int retryCount; public Retry(int retryCount) { this.retryCount = retryCount; } public Statement … Read more