testing

Testing aiohttp client with unittest.mock.patch

倾然丶 夕夏残阳落幕 提交于 2021-02-20 09:19:53
问题 I've written a simple HTTP client using aiohttp and I'm trying to test it by patching aiohttp.ClientSession and aiohttp.ClientResponse . However, it appears as though the unittest.mock.patch decorator is not respecting my asynchronous code. At a guess, I would say it's some kind of namespacing mismatch. Here's a minimal example: from aiohttp import ClientSession async def is_ok(url:str) -> bool: async with ClientSession() as session: async with session.request("GET", url) as response: return

Testing aiohttp client with unittest.mock.patch

天大地大妈咪最大 提交于 2021-02-20 09:18:29
问题 I've written a simple HTTP client using aiohttp and I'm trying to test it by patching aiohttp.ClientSession and aiohttp.ClientResponse . However, it appears as though the unittest.mock.patch decorator is not respecting my asynchronous code. At a guess, I would say it's some kind of namespacing mismatch. Here's a minimal example: from aiohttp import ClientSession async def is_ok(url:str) -> bool: async with ClientSession() as session: async with session.request("GET", url) as response: return

ASP.NET Testing Api controller: Uri(Request.GetEncodedUrl()…) returns null

帅比萌擦擦* 提交于 2021-02-19 07:43:49
问题 I am testing an ASP.NET Core Api post method, but I get a System.NullReferenceException causing my test to fail. The null exception appears when I try to return a Created()-ActionResult, like this: return Created(new Uri(Request.GetEncodedUrl() + "/" + personDto.Id), personDto); My personDto.Id nor the personDto is not null, but the result of the Uri() returns null. I have googled but not found a solution, but I believe I have to mock the Uri()-method in some way (I am new to testing and asp

Testing parent methods in stateless child component in Jest

假如想象 提交于 2021-02-19 07:35:14
问题 I currently have a react component with an onChange method. I want to test the onChange method like if someone were to type in, or paste things in. The problem is the handleChange is dependent on the parent components using it. What's the best way to test for the simulation? Do I need to reference the parent component's handleChange method and simulate that? CodeSandbox (i didn't add the tests here) https://codesandbox.io/s/react-basic-example-cwdh1?from-embed Updated Current Test: const

Cannot use attach_mock with an autospec function mock

烈酒焚心 提交于 2021-02-19 07:27:19
问题 Library module: # mod.py def foo(): bar1("arg1") bar2("arg2x", "arg2y") def bar1(x): pass def bar2(x, y): pass Test module: # test_mod.py from mod import foo def test_foo(mocker): mock = mocker.MagicMock() mock.attach_mock(mocker.patch("mod.bar1"), "b1") mock.attach_mock(mocker.patch("mod.bar2", autospec=True), "b2") foo() mock.assert_has_calls( [ mocker.call.b1("arg1"), mocker.call.b2("arg2x", "arg2y"), ] ) The mocker fixture is from pytest-mock plugin. Execute the MCVE with python -m pytest

Remove empty line from a multi-line string with Java

旧时模样 提交于 2021-02-19 01:28:45
问题 I have a multi-line string and some empty lines between other lines. It looks like: def msg = """ AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE TEST FFFFF GGGGGG """ I tried some regex expression with : msg = msg.replaceAll('(\n\\\\s+\n)+', '') Or msg = msg.replaceAll('(\r?\n){2,}', '$1'); But nothing is good about what I'm looking... Is it possible to remove only empty lines? to get something like that : def msg = """ AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE TEST FFFFF GGGGGG """ 回答1: Use regex (?m)^[ \t]*\r

Remove empty line from a multi-line string with Java

元气小坏坏 提交于 2021-02-19 01:27:10
问题 I have a multi-line string and some empty lines between other lines. It looks like: def msg = """ AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE TEST FFFFF GGGGGG """ I tried some regex expression with : msg = msg.replaceAll('(\n\\\\s+\n)+', '') Or msg = msg.replaceAll('(\r?\n){2,}', '$1'); But nothing is good about what I'm looking... Is it possible to remove only empty lines? to get something like that : def msg = """ AAAAAA BBBBBB CCCCCC DDDDDD EEEEEE TEST FFFFF GGGGGG """ 回答1: Use regex (?m)^[ \t]*\r

Are there Parameterized Test Fixtures for xunit?

风格不统一 提交于 2021-02-18 22:15:45
问题 Does xunit.net support "Parameterized Test Fixtures" like nunit (see example code below). Note I'm not looking for IUseFixture<T> or [Theory] because these do not provide the same functionality as Parameterized Test Fixtures [TestFixture("hello", "hello", "goodbye")] [TestFixture("zip", "zip")] [TestFixture(42, 42, 99)] public class ParameterizedTestFixture { private string eq1; private string eq2; private string neq; public ParameterizedTestFixture(string eq1, string eq2, string neq) { this

What are Go example functions?

99封情书 提交于 2021-02-18 21:58:26
问题 The Go testing package mentions example functions as in: func Example() { ... } func ExampleF() { ... } func ExampleT() { ... } func ExampleT_M() { ... } What is the meaning and use case for these? 回答1: Example functions are usage examples of a package or functions or other code you're documenting. Example functions will be included in the generated godoc in source form (while other functions are not), with proper formatting, also some processing applied, for example if last line of the

Java Junit test HTTP POST request

六月ゝ 毕业季﹏ 提交于 2021-02-18 20:30:14
问题 I need to test the following method with out altering the method itself. The method makes a POST method to a server. But I need to make a test case that's independent from the server. I tested a similar method before redirecting it to a local file. But that for that I was giving protocol as file, hostname as localhost and port as -1. My problem is that this method does a post and casts to HttpURLConnection and wr = new DataOutputStream(conn.getOutputStream()); wont work on an local txt file