Posts

Showing posts with the label unit-testing

How to convert csv into mock when csv file is specified as argument in unit test of Django command

How to convert csv into mock when csv file is specified as argument in unit test of Django command There is a django command that gives the path of the csv file as an argument as follows. class Command(NoticeCommand): def add_arguments(self, parser): parser.add_argument( '--file', dest="file", type=str, required=True ) def handle(self, *args, **options): with open(options['file'], 'r') as f: reader = csv.reader(f) next(reader) for row in reader: ... I am thinking to make this csv file mock when doing unit test. However, I do not know which part and how to make mock. Also, the argument is required = True . How can I call UnitTest when csv is mocked? required = True from mock import patch from django.core.management import call_command class ImportCsvTest(TestCase): @patch("common.management.commands.import_csv.????") def test_import_csv(self...

how to test that a method throws an exception junit5 [duplicate]

how to test that a method throws an exception junit5 [duplicate] This question already has an answer here: I have a DocumentTypeDetector class that has a detectForRequest() method. I'm doing the corresponding tests, but I have not been able to verify that a customized exception is thrown, I'm using JUNIT 5. DocumentTypeDetector detectForRequest() I have reviewed here, but the answers have not helped me, this is the code I have written following an example: @Test public void tryThrowExceptionForInvalidRequest() throws Exception{ InvalidInputRequestType exceptionThrown = Assertions.assertThrows( InvalidInputRequestType.class, () -> { throw new InvalidInputRequestType("La petición debe estar en un formato valido JSON o XML"); } ); assertEquals("La petición debe estar en un formato valido JSON o XML", exceptionThrown.getMessage()); } But this does not tell me anything about my test I need to ve...

Throw exception in test method using MSTest

Throw exception in test method using MSTest I'm using MSTest to write test cases for my application. I have a method where files are moved from one directory to another directory. Now when I run code coverage, it shows that the catch block is not covered in code coverage. This is my code as below. class Class1 { public virtual bool MoveFiles( string fileName) { bool retVal = false; try { string sourcePath = "PathSource"; string destinationPath = "DestPath"; if (Directory.Exists(sourcePath) && Directory.Exists(destinationPath)) { string finalPath = sourcePath + "\" + fileName ; if (Directory.Exists(finalPath)) { File.Move(finalPath, destinationPath); retVal = true; } } } ...

test timers in react, sinon or jest

test timers in react, sinon or jest I have a funtion checkIdleTime , being set to be called periodically. checkIdleTime componentDidMount() { var idleCheck = setInterval(this.checkIdleTime.bind(this), authTimeoutSeconds * 1000); this.setState({idleCheck: idleCheck}); document.onkeypress = this.setActive; } I want to use the fake timer for test, but can't figure out how, tried sinon and jest . sinon jest beforeEach(() => { checkIdleTime = jest.spyOn(PureMain.prototype, 'checkIdleTime'); wrapper = shallow( <PureMain/> ); jest.useFakeTimers(); //clock = sinon.useFakeTimers(); }); it('should check the idle time after [authTimeoutSeconds] seconds of inactivity', () => { wrapper.instance().componentDidMount(); var idleCheck_timeout = wrapper.instance().state.idleCheck; expect(idleCheck_timeout).not.toEqual(null); expect(idleCheck_timeout._idleTimeout).toBe(authTimeoutSeconds * 1000); jest.runAllTi...

Testing with Mocha and Chai consuming values from previous tests

Testing with Mocha and Chai consuming values from previous tests I have some tests created with mocha and chai using TypeScript , they actually work as expected. Each function returns a Promise which runs a test . mocha chai TypeScript Promise test My question is if is there anyway to consume on each test the value returned by a previous test without using the nesting you see below. test test My concern is that if I have more test the nesting code could be very annoying going to the right test import * as request from 'supertest'; import app from '../src/app'; import { Promise } from 'bluebird'; import * as dateformat from 'dateformat'; import Commons from '../../utils/commons'; import { expect } from 'chai'; ... // all the functions used below are defined over here ... registerNonExistingUser(email, pass, role).then( (jwtToken: string) => { authenticateUserCorrectJwt(jwtToken).then( (user) => { ...

Running multiple testbenches for VHDL designs

Running multiple testbenches for VHDL designs Whenever I create a VHDL design I tend to have many modules. Each of these modules are then connected to one main file and so everything is synthesised. But I want to write seperate test benches for each of these modules and one for the global process. It would be nice if I could do something to link all of these testbenches together and make them run in succession, to test my entire design in one run. How could I do this? I like to use GHDL and asserts. Is it possible to create one super-testbench? Or would a shell script which iterates over them be better? Good strategy. There are unit testing tools for VHDL too ... one called VUnit for example : github.com/LarsAsplund/vunit – Brian Drummond Jan 23 '16 at 23:20 @Paebbels, you are right. F...

angular 2 test URL differentiates from web API URL

angular 2 test URL differentiates from web API URL I have an angular-cli project, embedded in visual studio 2017. By running vs project the angular project is being served and everything works. then when I want to test the angular project with ng test , it will be served on http://localhost:9876 but API is unavailable on this URL. ng test http://localhost:9876 Web API URL is http://localhost:42339/ . and test fails because request goes to http://localhost:9876 . I changed the service URLs http://localhost:42339/ http://localhost:9876 getTagData(): Observable<any> { return this.adminBaseService.get(`http://localhost:42339/api/admin/Tags/GetTags`); } but it makes calls to http://localhost:9876 http://localhost:9876 Any help? I think you should define route in angular project. this url is for backend. you need define a component and service for http. and use this service in component. then define a route for the component. –...