Posts

Showing posts with the label unit-testing

How does one test net.Conn in unit tests in Golang?

Image
Clash Royale CLAN TAG #URR8PPP How does one test net.Conn in unit tests in Golang? I'm currently looking into creating some unit tests for net.Conn interface in Go, as well as other functions that build up on top of that functionality, and I'm wondering what is the best way to unit test that in Google Go? My code looks like: conn, _:=net.Dial("tcp", "127.0.0.1:8080") ... fmt.Fprintf(conn, "test") ... buffer:=make(byte, 100) conn.Read(buffer) Is the most efficient way of testing this code and the code that uses these functions to spin up a separate goroutine to act like the server, use net.http.httptest package, or something else? Suggest reading the source for the tests for the actual net library. I have picked up plenty of tips by doing that in the past. Secondly as you have already mentioned use the httptest package. – miltonb Jun 7 '15 at 4:31 ...

Mocha tests are ignored when used within repetitive stream callbacks

Image
Clash Royale CLAN TAG #URR8PPP Mocha tests are ignored when used within repetitive stream callbacks My task is to test some js function against big test vector corpus (10M+), also requirement to test in browser forced me to stream testvectors from local webserver, rather than synchronously read from file record by record. So, I'm obtaining (one per line ascii) test vectors this way: const http = require('http'); const readline = require('readline'); FetchTestVectors = function(filename, recordCb) { const url = "http://localhost:8080/" + filename; http.get(url) .on('response', function(response) { readline.createInterface({ input: response }) .on('line', function(line) { //recordCb(line.split(':')); recordCb(line); }) }); } And when trying to test it, inner describe-it block becomes ignored (all callbacks). However, I can see...

How to unit test a POST method in python?

Image
Clash Royale CLAN TAG #URR8PPP How to unit test a POST method in python? I have a method that sends a POST containing a JSON to an Elasticsearch instance. I am trying to write a unit test that verify the contents of the sent JSON, but I am not sure how to go about that. Should I create a local server in python and have it verify the contents of the POST or something else? I currently have this: class TestAnalytics(BaseTest): def test_post(self): info = {"test1": "value1", "test2": "value2"} resp = requests.post(config.tool_repo_urls['es_url'], data=json.dumps(info), headers={'Content-Type': 'application/json'}) assert_equal(resp.status_code, 200) # verify valid response code Create a unit test to POST and check the result of the POST based on the returned value from the API and then run a GET to confirm the contents match what you POSTed? – dfunda...

Jest + Enzyme + React Native: How to test content of tag?

Image
Clash Royale CLAN TAG #URR8PPP Jest + Enzyme + React Native: How to test content of <Text /> tag? I want to unit test with Jest and Enzyme if my <Text /> tag correctly receives props.header as text. <Text /> props.header Usually I was able to test the content of the <Text /> tag like this: <Text /> it("should render a label", () => { expect(wrapper.find(Text).contains("submit")).toBe(true); }); But as soon as I pass an object this is no longer possible. Let me show you: const createTestProps = props => ({ header: SOME_CONSTANT, ...props }); ... let wrapper; let props; beforeEach(() => { props = createTestProps(); wrapper = shallow(<MyList {...props} loaded={false} />); }); it("should render a header", () => { expect(wrapper.find(Text).contains(props.header)).toBe(true); }); This fails with the following error message: ● MyList › rendering › still loading › should render a head...

HttpPost does not get ContentLength from PostAsync request

Image
Clash Royale CLAN TAG #URR8PPP HttpPost does not get ContentLength from PostAsync request I have a unit test code that sends request to an HttpPost method. [Test] public async Task ValidateToken() { var content = new FormUrlEncodedContent(new { new KeyValuePair<string, string("test", "test") }; httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); var response = await httpClient.PostAsync("/api/token/1", content); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); } [Route("api/token")] public class MyController : BaseApiController { private readonly HttpRequest httpRequest; public MyController(IHttpContextAccessor httpContextAccessor) { httpRequest = httpContextAccessor.HttpContext.Request; } [HttpPost("{count}")] public async Task<IActionResult> Post(int count) { if...

Set JUnit timeout in eclipse

Set JUnit timeout in eclipse Question When I run all our JUnit tests, using eclipse, can I set a default timeout? Background My manager insists on writing Unit tests that sometimes take up to 5 minutes to complete. When I try to run our entire test suite (only about 300 tests) it can take over 30 minutes. I want to put something in place that will stop any test that takes longer than 10 seconds. I know an individual test can be annotated with: @Test(timeout=10000) But doing this would make his long tests always fail. I want them to work when he runs them on his box (if I have to make minor adjustments to the project before checking it in, that's acceptable. However, deleting the timeouts from 40 different test files is not practical). I also know I can create an ant task to set a default timeout for all tests, along the lines of: <junit timeout="10000"> ... </junit> The problem with that we typically run our tests from inside eclipse with Right Click >...